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);
}
}];
}