PHAssetChangeRequest failing for UIImage taken with camera
Asked Answered
L

2

10

I'm having an issue implementing a camera View Controller for an iOS app that I'm working on. When a button is pressed, I have a singleton camera object that manages the AVFoundation side of things and captures the image. As you can see below, once the UIImage is captured from the camera, I pass a completion block that utilizes it.

The first method below is an action that fires when the capture button is pressed on the camera. First, the camera layer is paused by disabling the connection on the preview layer, then the image is captured. Then, the camera object captures the UIImage, at which point I remove the camera preview layer from the view and instead add a UIImageView subview with the captured image in its place.

Next, I want to add the image to the album I have created in Photos using the Photos framework. I can create the album without an issue, and I have verified that the PHAssetCollection object. I am using in the second method is the correct one.

For some reason, however, I can't add the UIImage I capture to the album. I tried to add a random image file I had in my project to the album, and the operation completed successfully. I have also verified that the correct image is successfully being passed to the addPhotoToSavedPhotos method by using NSLog statements to check the image description in both methods. This leads me to believe that something is wrong with the image somehow, but the imageView displays it successfully, so I'm not sure what that might be.

If anyone has any ideas of solutions I might try, I would appreciate it. Also the error.localizedDescription from the NSLog statement in the second method outputs "The operation couldn't be completed. (Cocoa error -1)."

- (IBAction)capturePhoto:(id)sender {

    [self pauseCameraLayer];
    [[eventCamera sharedInstance] captureStillUIImage:^(UIImage *image, NSError *error){

        if(error)
        {
            NSLog(@"error capturing photo");
        }
        else
        {
            NSLog(@"%@",image.debugDescription);
        }
        [_captureButton setHidden:YES];
        [_switchCameraButton setHidden:YES];
        UIImageView *preview=[[UIImageView alloc] initWithImage:image];
        [preview setAutoresizesSubviews:YES];
        [preview setContentMode:UIViewContentModeScaleAspectFit];
        [preview setTransform:CGAffineTransformMakeRotation(M_PI_2)];
        preview.frame=_imageView.bounds;
        [_imageView addSubview:preview];
        [self addPhotoToSavedPhotos:[image copy]];
        NSLog(@"1: %@",image.description);
    }];

-(void)addPhotoToSavedPhotos:(UIImage*)photo
{
    PHAssetCollection *myCollection=[self getAPPCollection];
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        //Perform changes to photo library

        PHAssetChangeRequest *req=[PHAssetChangeRequest creationRequestForAssetFromImage:photo];
        PHAssetCollectionChangeRequest *assetChangeRequest=[PHAssetCollectionChangeRequest changeRequestForAssetCollection:myCollection];
        [assetChangeRequest addAssets:[NSArray arrayWithObject:req.placeholderForCreatedAsset]];
        //[libReq addAssets:@[assetPlaceHolder]];

    }completionHandler:^(BOOL success, NSError *error){
        //Perform any necessary actions after adding the photo to the photo 
        //library
        if(!success)
        {
            NSLog(@"didn't succeed, error: %@",error.localizedDescription);
        }
    }];
}
Lashkar answered 11/1, 2017 at 6:44 Comment(3)
do you fix that?Terribly
can you please tell us what is printed in the log?Secundas
There may be an error somewhere here I guess self addPhotoToSavedPhotos:[image copy]]; Try adding a breakPoint in -(void)addPhotoToSavedPhotos:(UIImage*)photo function and check if there is a photo being passed into this function.Fastidious
W
1

Try Something like this This Code will Check wheather album Exist or not first

  #import <Photos/Photos.h>

    - (void)addPhotoToSavedPhotos:(UIImage *)image {
        NSString *albumName = @"NameOFTheAlbum";

        void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
                PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];

            } completionHandler:^(BOOL success, NSError *error) {
                if (!success) {
                    NSLog(@"Error creating asset: %@", error);
                }
            }];
        };

        PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
        fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName];
        PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
        if (fetchResult.count > 0) {
            saveBlock(fetchResult.firstObject);
        } else {
            __block PHObjectPlaceholder *albumPlaceholder;
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
                albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;

            } completionHandler:^(BOOL success, NSError *error) {
                if (success) {
                    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
                    if (fetchResult.count > 0) {
                        saveBlock(fetchResult.firstObject);
                    }
                } else {
                    NSLog(@"Error creating album: %@", error);
                }
            }];
        }
    }
Wakeful answered 8/8, 2017 at 6:5 Comment(0)
V
0

PHAssetChangeRequest pops a permission dialog. UI must always be in the main thread.

Try moving the PHAssetChangeRequest out of the callback.

Virility answered 8/8, 2017 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.