0

My scenario is that I select one of the photos fetched from system album with PhotoKit and presented in my UICollectionView and pass the selected photo (UIImage) to my next UIView and send it to remote server in form of NSData. But when I put breakpoint before sending to track the NSData I found that the UIImage had data with allocated memory while NSData did not.

Here is the code to fetch the UIImage (Please notice that I didn't specify any PHImageRequestOptions object):

NSMutableArray *images = @[].mutableCopy;
PHImageManager *manager = [PHImageManager defaultManager];
PHAsset *asset = _photoPickerCollectionView.pickedAsset;
CGSize targetSize = CGSizeMake(asset.pixelWidth*0.5, asset.pixelHeight*0.5);
[manager requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info) {
    _nextView.pickedOne = result;
}];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];

I convert the UIImage to NSData like this:

UIImage *image = _pickedOne;
NSData *imgData = UIImageJPEGRepresentation(image, imageQuality);

When I tracked the variables, the UIImage contains data as below:

enter image description here

But the NSData is nil as below:

enter image description here

And what was ODD is that if I specified an PHImageRequestOptions object for the PHImageManager to request images, the NSData wouldn't be nil. I'm not sure what was changed with or without the PHImageRequestOptions and why it would make such difference.

UPDATE: What I found is that if you specify PHImageRequestOptions to nil then the default options will force it to fetch photos asynchronously which, in my opinion, can be unstable for NSData, so when I specify options.synchronous = YES; it would work.

But in this case, would it cause any retain cycle or some PhotoKit objects won't get released?

4

1 に答える 1

0

デフォルトのメソッドは非同期です。画像のフェッチを同期的に明示的に行うには、オプションを設定する必要があります。この種の変換に対する私の強い提案は、次の API を使用することです。

- (PHImageRequestID)requestImageDataForAsset:(PHAsset *)asset options:(nullable PHImageRequestOptions *)options resultHandler:(void(^)(NSData *__nullable imageData, NSString *__nullable dataUTI, UIImageOrientation orientation, NSDictionary *__nullable info))resultHandler;

これによりデータが直接返されますが、これも同期する必要があります。これにより、オブジェクトの保持サイクルが発生することはありません。お役に立てれば。:)

于 2015-09-24T06:03:30.453 に答える