본문으로 바로가기

[AssetLibrary] 원본 이미지 가져오기.

category Mobile Application/iOS 2015. 3. 1. 20:29

* fullResolutionUnadjustedImage는 원본 이미지 그대로, fullResolutionAdjustedImage는 필터가 적용된 이미지를 반환한다.

- (UIImage *)_fullResolutionUnadjustedImage {
    ALAssetRepresentation *representation = [_asset defaultRepresentation];
    CGImageRef imageRef = [representation fullResolutionImage];
    UIImageOrientation orientation = (UIImageOrientation)representation.orientation;
    return [UIImage imageWithCGImage:imageRef scale:[representation scale] orientation:orientation];
}

- (UIImage *)_fullResolutionAdjustedImage {
    ALAssetRepresentation *representation = [_asset defaultRepresentation];
    
    // WARNING: This code doesn't work for iOS 8.0+. Use PhotosKit instead.

    CGImageRef imageRef = [representation fullResolutionImage];
    NSString *adjustment = [[representation metadata] objectForKey:@"AdjustmentXMP"];
    if (adjustment != nil) {
        NSData *xmpData = [adjustment dataUsingEncoding:NSUTF8StringEncoding];
        CIImage *image = [CIImage imageWithCGImage:imageRef];
        
        NSError *error = nil;
        NSArray *filterArray = [CIFilter filterArrayFromSerializedXMP:xmpData inputImageExtent:image.extent error:&error];
        CIContext *context = [CIContext contextWithOptions:nil];
        if (filterArray != nil && !error) {
            for (CIFilter *filter in filterArray) {
                [filter setValue:image forKey:kCIInputImageKey];
                image = [filter outputImage];
            }
            // TODO: Fix crash when OpenGL calls are made in the background
            imageRef = [context createCGImage:image fromRect:[image extent]];
        }
    }
    UIImageOrientation orientation = (UIImageOrientation)representation.orientation;
    return [UIImage imageWithCGImage:imageRef scale:[representation scale] orientation:orientation];
}