Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top