iosobjective-ccocoa-touchuikitalassetlibrary

AssetLibrary - how to load with orientation correction?


I've created a picture indexing service (local, Facebook, Picassa, Instagram, etc).

In the collection view, a double-tap will pop the image out of the cell to become full-sized. For local sources, it sometimes uses the wrong orientation. . How can I force it to use the orientation of the device (landscape), even if the photo makes a better fit in potrait mode?

Here's the method to load the indexed asset from the local assets library:

- (void)performLoadFor:(GTPImage*)image onSuccess:(void (^)(UIImage*))onSuccess onError:(void (^)(NSError*))onError
{
    [_assetsLibrary assetForURL:[NSURL URLWithString:image.address] resultBlock:^(ALAsset* asset)
    {
        if (onSuccess)
        {
            CGImageRef imageRef = [[asset defaultRepresentation] fullResolutionImage];
            onSuccess([UIImage imageWithCGImage:imageRef]);
        }
    } failureBlock:^(NSError* error)
    {
        onError(error);
    }];
}

Solution

  • This article explains it very well: http://biasedbit.com/alasset-image-orientation/

    This is how you get image orientation from an asset:

    // Retrieve the image orientation from the ALAsset
    UIImageOrientation orientation = UIImageOrientationUp;
    NSNumber* orientationValue = [asset valueForProperty:@"ALAssetPropertyOrientation"];
    if (orientationValue != nil) {
        orientation = [orientationValue intValue];
    }
    

    You could also try using [ALAssetRepresentation orientation] and create image like this:

    UIImage* image = [UIImage imageWithCGImage:ref scale:1.0f orientation:(UIImageOrientation)[rep orientation]];
    

    Seems to work for me.