iOS AssetsLibrary和Photos的使用总结(2): 原图获取

来源:互联网 发布:软件测试难不难 编辑:程序博客网 时间:2024/04/30 18:57

源码下载:https://github.com/darling0825/DLPhotoPicker.git
封装了AssetsLibrary和Photos的API,支持iOS7和iOS8以上

- (UIImage *)originImage{    @autoreleasepool {        if (_originImage) {            return _originImage;        }        __block UIImage *resultImage;        if (UsePhotoKit) {            //  image after edited            PHImageRequestOptions *originRequestOptions = [[PHImageRequestOptions alloc] init];            originRequestOptions.version = PHImageRequestOptionsVersionCurrent;            originRequestOptions.networkAccessAllowed = YES;            originRequestOptions.synchronous = YES;            //sync requests are automatically processed this way regardless of the specified mode            //originRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;            originRequestOptions.progressHandler = ^(double progress, NSError *__nullable error, BOOL *stop, NSDictionary *__nullable info){                dispatch_main_async_safe(^{                });            };            [[[DLPhotoManager sharedInstance] phCachingImageManager]            requestImageForAsset:self.phAsset            targetSize:PHImageManagerMaximumSize            contentMode:PHImageContentModeDefault            options:originRequestOptions            resultHandler:^(UIImage *result, NSDictionary *info) {                @autoreleasepool {                                                                                      BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue] && result;                    if (downloadFinined) {                        resultImage = result;                    }                }            ];        } else {            CGImageRef fullResolutionImageRef = [[self.alAsset defaultRepresentation] fullResolutionImage];            NSString *adjustment = [[[self.alAsset defaultRepresentation] metadata] objectForKey:@"AdjustmentXMP"];            if (adjustment) {                NSData *xmpData = [adjustment dataUsingEncoding:NSUTF8StringEncoding];                CIImage *tempImage = [CIImage imageWithCGImage:fullResolutionImageRef];                NSError *error;                NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData                                                             inputImageExtent:tempImage.extent                                                                        error:&error];                CIContext *context = [CIContext contextWithOptions:nil];                if (filterArray && !error) {                    for (CIFilter *filter in filterArray) {                        [filter setValue:tempImage forKey:kCIInputImageKey];                        tempImage = [filter outputImage];                    }                    fullResolutionImageRef = [context createCGImage:tempImage fromRect:[tempImage extent]];                }            }            resultImage = [UIImage imageWithCGImage:fullResolutionImageRef scale:[[self.alAsset defaultRepresentation] scale] orientation:(UIImageOrientation)[[self.alAsset defaultRepresentation] orientation]];        }        _originImage = resultImage;        return resultImage;    }}- (NSInteger)requestOriginImageWithCompletion:(void (^)(UIImage *, NSDictionary *))completion                          withProgressHandler:(PHAssetImageProgressHandler)phProgressHandler{    @autoreleasepool {        if (UsePhotoKit) {            if (_originImage) {                dispatch_main_async_safe(^{                    if (completion) {                        completion(_originImage, nil);                    }                })                return 0;            } else {                PHImageRequestOptions *originRequestOptions = [[PHImageRequestOptions alloc] init];                originRequestOptions.version = PHImageRequestOptionsVersionCurrent;                originRequestOptions.networkAccessAllowed = YES;                originRequestOptions.progressHandler = phProgressHandler;                self.imageRequestID = [[[DLPhotoManager sharedInstance] phCachingImageManager]                                       requestImageForAsset:self.phAsset                                       targetSize:PHImageManagerMaximumSize                                       contentMode:PHImageContentModeDefault                                       options:originRequestOptions                                       resultHandler:^(UIImage *result, NSDictionary *info) {                                               BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];                                               if (downloadFinined) {                                                   _originImage = result;                                               }                                               if (completion) {                                                   completion(result, info);                                               }                                           }                                       }];                return self.imageRequestID;            }        } else {            dispatch_main_async_safe(^{                if (completion) {                    completion([self originImage], nil);                }            })            return 0;        }    }}
0 0