Question

I am fetching a ALAsset from the library, but when I try to set the an UIImageView the UIImage is nil.

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:[NSURL URLWithString:entityObject.localUrl] resultBlock:^(ALAsset *asset) {
        if (asset) {
            ALAssetRepresentation *representation = [asset defaultRepresentation];
            imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                       scale:[representation scale]
                                 orientation:(UIImageOrientation)[representation orientation]];

            NSLog(@"imageView.image: %@",imageView.image); // imageView.image: (null)
            NSLog(@"image size %f", imageView.image.size.width); //image size: 0.000000
            imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.image.size.width, imageView.image.size.height);

        } else {
            NSLog(@"test not found?");
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"FAILED TO FIND %@", error);
    }];

any idea what i am doing wrong?

Was it helpful?

Solution

Your code looks great, which leads me to suspect that the problem is someplace you are not looking - namely, perhaps imageView itself is nil. This causes imageView.image to be nil, and so you are imagining that the call to [UIImage imageWithCGImage...] is failing. But it isn't!

The moral here is: unravel your code a bit more. You wrote this:

imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                   scale:[representation scale]
                             orientation:(UIImageOrientation)[representation orientation]];

That's what's hiding the real issue. If only you had written this:

UIImage* image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                   scale:[representation scale]
                             orientation:(UIImageOrientation)[representation orientation]];
NSLog(@"%@", image);
imageView.image = image;
// ...

... it would have been perfectly clear that you were successfully fetching the image from the asset, but then when you tried to assign it to the image view, the image view was not there to receive it and that was where the ball got dropped.

OTHER TIPS

Like this works for me:

ALAssetsLibrary *library = [self defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

        if(result)
        {
            ALAssetRepresentation *representation = [result defaultRepresentation];
            imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                                  scale:[representation scale]
                                            orientation:(UIImageOrientation)[representation orientation]];

            NSLog(@"imageView.image: %@",imageView.image); // imageView.image: (null)
            NSLog(@"image size %f", imageView.image.size.width); //image size: 0.000000
            imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.image.size.width, imageView.image.size.height);

        }
    }];

} failureBlock:^(NSError *error) {
    NSLog(@"Error loading images %@", error);
}];

- (ALAssetsLibrary *)defaultAssetsLibrary {
        static dispatch_once_t pred = 0;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^{
            library = [[ALAssetsLibrary alloc] init];
        });
        return library;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top