我在为我正在处理的iOS应用实施相机View Controller时遇到问题。当按下一个按钮时,我有一个单独的相机对象来管理事物的AVFoundation
面并捕获图像。如下所示,一旦从相机捕获UIImage
,我就会通过一个使用它的完成块。
下面的第一种方法是在相机上按下捕获按钮时触发的动作。首先,通过禁用预览图层上的连接暂停摄像机图层,然后捕获图像。然后,相机对象捕获UIImage
,此时我从视图中移除相机预览图层,而是添加UIImageView
子视图,并将捕获的图像放在其位置。
接下来,我想使用Photos框架将图像添加到我在Photos中创建的相册中。我可以毫无问题地创建相册,并且我已经验证了PHAssetCollection
对象。我在第二种方法中使用的是正确的方法。
但出于某种原因,我无法将UIImage
我捕获到相册中。我尝试将我项目中的随机图像文件添加到相册中,操作成功完成。我还验证了通过使用addPhotoToSavedPhotos
语句检查两种方法中的图像描述,成功将正确的图像传递给NSLog
方法。这让我相信图像不知何故出了问题,但是imageView成功地显示了它,所以我不确定它可能是什么。
如果有人对我可能会尝试的解决方案有任何想法,我将不胜感激。此外,第二种方法中error.localizedDescription
语句的NSLog
输出"The operation couldn't be completed
。 (可可错误-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);
}
}];
}
答案 0 :(得分:1)
尝试这样的事情 此代码将检查天气专辑是否存在
#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);
}
}];
}
}
答案 1 :(得分:0)
PHAssetChangeRequest弹出权限对话框。 UI必须始终位于主线程中。
尝试将PHAssetChangeRequest移出回调。