AssetLibrary - 如何加载方向校正?

Jas*_*ues 2 cocoa-touch objective-c uikit ios alassetlibrary

我创建了一个图片索引服务(本地,Facebook,Picassa,Instagram等).

在集合视图中,双击会将图像弹出单元格以变为全尺寸.对于本地来源,它有时使用错误的方向..即使照片更适合potrait模式,如何强制它使用设备的方向(横向)?

以下是从本地资产库加载索引资产的方法:

- (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);
    }];
}
Run Code Online (Sandbox Code Playgroud)

Lev*_*Lev 5

这篇文章解释得非常好:http: //biasedbit.com/alasset-image-orientation/

这是您从资产获取图像方向的方式:

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

您也可以尝试使用[ALAssetRepresentation orientation]并创建如下图像:

UIImage* image = [UIImage imageWithCGImage:ref scale:1.0f orientation:(UIImageOrientation)[rep orientation]];
Run Code Online (Sandbox Code Playgroud)

似乎为我工作.