Saving image using Photos.Framework performChanges application crashing

I am using uiimagepickercontroller to get photos from the camera and then the following function is called. When the executeChanges block is called, there is a spike in memory usage and if many photos are executed quickly, the application crashes. Also, not all photos are saved.

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     UIImage* originalImage =[info objectForKey:UIImagePickerControllerOriginalImage];

     CGSize destinationSize = self.shutterButton.frame.size;
     UIGraphicsBeginImageContext(destinationSize);
     [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

     [self.shutterButton setBackgroundImage:newImage forState:UIControlStateNormal];
    //[self saveImage:originalImage];
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:newImage];

        //the if statement below should be:if (self.assetCollection)
        if (self.assetCollection) {
            PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
            [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
        }
    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating asset: %@", error);
        }
    }];
}

      

+3


source to share





All Articles