ALAssetsLibrary 图像扭曲 [英] ALAssetsLibrary image twisted

查看:22
本文介绍了ALAssetsLibrary 图像扭曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从相机胶卷加载图像.但是当图像显示在 uiimageview 中时,图像有时会向左旋转 90 度.我该如何纠正?谢谢!

I'm using the following code to load images form the camera roll. But when the images are shown in an uiimageview, the images are sometimes rotated by 90 degrees to the left. How can I correct this? Thanks!

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]];
            });

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

the app crashes and I get the following error:

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

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

I may have should said, I'm using the code inside a cellForItemAtIndexPath method.

更新 2:

通过在更新 ui 时不使用主队列,我能够避免该错误.我这里的小节点,上面的代码为给定的单元格加载了错误的图像.很奇怪.

I were able to avoid the error by not usind the main queue when updating the ui. I little side node here, the code above is loading wrong images for the given cells. Very weird.

推荐答案

您必须将您的方法编辑为

You have to edit your method as

if (iref) {

  dispatch_async(dispatch_get_main_queue(), ^{

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

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

This above code will retrieve the asset images with their captured/original orientation.

您必须更新主线程上的用户界面以获得更好的性能.所以你必须使用任何一个

You have to update the user interface on main thread for better performance. So you have to use either

dispatch_async(dispatch_get_main_queue()

performSelectorOnMainThread

这篇关于ALAssetsLibrary 图像扭曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆