Wednesday, October 16, 2013

ALAssetsLibraryAssetForURLResultBlock return value

/ / Export images to a specified path
- (UIImage *) exportImage: (NSURL *) assetPhotoURL: (int) imageType
{
ALAssetsLibraryAssetForURLResultBlock resultblock = ^ (ALAsset * myasset)
{
CGImageRef imageRef;

if (imageType == SIZE_TYPE_THUMBNAIL)
{
imageRef = [myasset thumbnail];
}
else
{
ALAssetRepresentation * assetRepresentation = [myasset defaultRepresentation]; / / Big Picture
if (imageType == SIZE_TYPE_FULL_SCREEN)
imageRef = [assetRepresentation fullScreenImage];
else if (imageType == SIZE_TYPE_FULL_RESOLUTION)
imageRef = [assetRepresentation fullResolutionImage];
}

if (imageRef)
{
UIImage * image = [UIImage imageWithCGImage: imageRef];
NSLog (@ "Get Picture success");

/ / How to get pictures in return
}
};

ALAssetsLibraryAccessFailureBlock failureblock = ^ (NSError * error)
{
NSLog (@ "Get picture failed");
};

/ / Get the picture
ALAssetsLibrary * assetsLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[AssetsLibrary assetForURL: assetPhotoURL resultBlock: resultblock failureBlock: failureblock];

return nil;
}


The above code can get to the url specified repository picture object, but how to modify the code so that in their access to UIImage object can be returned directly, rather than through commissioned to return?
Reply:
You can use keyword __block, declare a variable to change its value in the block:
 
- (UIImage *) exportImage: (NSURL *) assetPhotoURL: (int) imageType
{
__block UIImage * image = nil;
ALAssetsLibraryAssetForURLResultBlock resultblock = ^ (ALAsset * myasset)
{
......
image = xxx;
}
return image;
}

Reply:
3rd Floor reply quote:
can use keywords __block, declare a variable to change its value in the block:
C / C + + code

- (UIImage *) exportImage: (NSURL *) assetPhotoURL: (int) imageType
{
__block UIImage * image = nil;
ALAssetsLibraryAssetForURLResultBlock resultblock = ......


This method does not work, I tried before, or will be returned empty UIImage

No comments:

Post a Comment