vijay
vijay

Reputation: 1235

App crashes with memory Pressure every time in IOS?

I am generating image from view using following functions

-(void)preparePhotos 
{
    [assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
     {
         if(result == nil)
         {
             return;
         }

         NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
         UIImage *img = [[UIImage imageWithCGImage:[[result defaultRepresentation] fullResolutionImage]] resizedImageToSize:CGSizeMake(600, 600)];
         [workingDictionary setObject:img forKey:@"UIImagePickerControllerOriginalImage"];
         [appdelegate.arrImageData addObject:workingDictionary];
     }]; 
}

But as the number of times this function is called is get increased , app get crashed. How can I optimize this function or any alternative function to get image from device gallery which will not result into crash.function call like this.

[self performSelectorInBackground:@selector(preparePhotos) withObject:nil];

Upvotes: -1

Views: 282

Answers (1)

Ajay
Ajay

Reputation: 1622

If you want to fetch all the images from photo library than you should only store their Assets urls only not the images themselves. Lets say you are storing the Assets url in an array named photoAssets than you can call this method by passing just the index:

- (UIImage *)photoAtIndex:(NSUInteger)index
{
    ALAsset *photoAsset = self.photoAssets[index];

    ALAssetRepresentation *assetRepresentation = [photoAsset defaultRepresentation];

    UIImage *fullScreenImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage]
                                                   scale:[assetRepresentation scale]
                                             orientation:UIImageOrientationUp];
    return fullScreenImage;
}

and for more information or reference you should refer PhotoScroller and MyImagePicker

Upvotes: 1

Related Questions