Unable to read asynchronous PHAsset content

I want to read the specified file (photo from the camera) asynchronously, but for me this does not work.

The variable tempData gets nil until I change the requestOptionForPhotos.synchronous configuration to YES , then everything will be fine, but I do not want to execute this code synchronously.

Is it possible that I block access to the photo by requesting the same file in another thread? I'm new to objective-c programming and iOS, and I don't know how this works.

  NSURL *assetUrl = [[NSURL alloc] initWithString:filepath]; PHFetchResult *collection = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:assetUrl] options:nil]; PHImageRequestOptions *requestOptionForPhotos = [[PHImageRequestOptions alloc] init]; requestOptionForPhotos.networkAccessAllowed = YES; requestOptionForPhotos.synchronous = NO; __block BOOL isFinished = NO; __block NSData * tempData = nil; for(PHAsset *asset in collection) { [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFill options:requestOptionForPhotos resultHandler:^(UIImage *result, NSDictionary *info) { tempData = UIImagePNGRepresentation(result); isFinished = YES; }]; } 
+5
source share
1 answer
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^{ NSURL *assetUrl = [[NSURL alloc] initWithString:filepath]; PHFetchResult *collection = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:assetUrl] options:nil]; PHImageRequestOptions *requestOptionForPhotos = [[PHImageRequestOptions alloc] init]; requestOptionForPhotos.networkAccessAllowed = YES; requestOptionForPhotos.synchronous = NO; __block BOOL isFinished = NO; __block NSData * tempData = nil; for (PHAsset *asset in collection) { [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFill options:requestOptionForPhotos resultHandler:^(UIImage *result, NSDictionary *info) { tempData = UIImagePNGRepresentation(result); isFinished = YES; }]; } }); 

try this code to get an asynchronous image and place a breakpoint on the result handler to check the weather you get the image or not.

0
source

Source: https://habr.com/ru/post/1245768/


All Articles