Image loading with GCD receiving memory warning

2019-07-23 12:05发布

问题:

I'm developing a photo gallery application using AssetsLibrary to load my device photos. When presenting a random image in another VC I've noticed the following : it takes about 1 or 2 seconds for my full res image to load on the imageView (way much longer than the native photosApp) and I also get from the log "Received memory warning" after loading a few images. If I set my representation to fullScreenImage the warnings stop but I don't want this. What must I change for a smooth performance and high quality images on the view ?

Here's the code,hope you can tell me what's the problem :

This is the VC where I want to present my image on the screen

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@",assetsController);

    detailImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:detailImageView];
    detailImageView.image = smallImage; //small image is my asset thumbnail and is passed as an argument in my init function

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        ALAsset *asset = [assetsController.albumPics objectAtIndex:assetsController.index];
        ALAssetRepresentation *representation = [asset defaultRepresentation];

        bigImage = [[UIImage imageWithCGImage:[representation fullResolutionImage]]retain];

        dispatch_async(dispatch_get_main_queue(), ^{

            detailImageView.image = bigImage;

        });
        [pool release];
    });
}

UPDATE 1

    {
        UIImageView *detailImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:detailImageView];
        detailImageView.image = smallImage;


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

            ALAsset *asset = [assetsController.albumPics objectAtIndex:assetsController.index];
            ALAssetRepresentation *representation = [asset defaultRepresentation];

            UIImage *bigImage = [UIImage imageWithCGImage:[representation fullResolutionImage]];


            dispatch_async(dispatch_get_main_queue(), ^{

                detailImageView.image = bigImage;

            });
            [pool release];

        });
}

回答1:

Is bigImage an instance variable? Is it used in any place other than here? If it is not used anywhere else, then it should be a local variable, and you shouldn't retain it. If it is an instance variable that you retain, you need to release the previous value before assigning a new value to it.

Same discussion applies to detailImageView