ALAssetsLibrary image twisted

丶灬走出姿态 提交于 2020-01-03 05:32:50

问题


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

Update:

When using the solution:

            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

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

Update 2:

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.


回答1:


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()

or

performSelectorOnMainThread


来源:https://stackoverflow.com/questions/20689538/alassetslibrary-image-twisted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!