ALAssetsLibrary图像扭曲

时间:2013-12-19 18:56:17

标签: ios uiimage alassetslibrary

我正在使用以下代码从相机胶卷加载图像。但是当图像以uiimageview显示时,图像有时会向左旋转90度。我怎么能纠正这个?谢谢!

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            cell.image.image = [UIImage imageWithCGImage:iref];
        }
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can't get image - %@",[myerror localizedDescription]);
    };


    NSString *urlString = [[_alleArtikel objectAtIndex:indexPath.row] bildURL];
    NSURL *asseturl = [NSURL URLWithString:urlString];
     ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
     [assetslibrary assetForURL:asseturl 
                    resultBlock:resultblock
                   failureBlock:failureblock];

更新

使用解决方案时:

            dispatch_async(dispatch_get_main_queue(), ^{
                cell.image.image =  [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
            });

应用程序崩溃,我收到以下错误:

invalid attempt to access <ALAssetRepresentationPrivate: 0x176a3350> past the lifetime of its owning ALAssetsLibrary

我可能应该说,我正在使用cellForItemAtIndexPath方法中的代码。

更新2:

我在更新ui时没有使用主队列来避免错误。我这里的小方节点,上面的代码加载给定单元格的错误图像。非常奇怪。

1 个答案:

答案 0 :(得分:1)

您必须将方法编辑为

if (iref) {

  dispatch_async(dispatch_get_main_queue(), ^{

       cell.image.image =  [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
  });
}

以上代码将检索具有捕获/原始方向的资产图像。

您必须更新主线程上的用户界面才能获得更好的性能。所以你必须使用

dispatch_async(dispatch_get_main_queue()

performSelectorOnMainThread