gpt4 book ai didi

ios - 使用相机拍摄的 UIImage 的 PHAssetChangeRequest 失败

转载 作者:技术小花猫 更新时间:2023-10-29 11:07:47 28 4
gpt4 key购买 nike

我在为我正在开发的 iOS 应用程序实现相机 View Controller 时遇到问题。按下按钮时,我有一个单例相机对象,用于管理事物的 AVFoundation 端并捕获图像。正如您在下面看到的,从相机捕获 UIImage 后,我传递了一个使用它的完成 block 。

下面的第一个方法是在按下相机上的拍摄按钮时触发的操作。首先,通过禁用预览层上的连接来暂停相机层,然后捕获图像。然后,相机对象捕获 UIImage,此时我从 View 中删除了相机预览层,而是添加了一个 UIImageView subview ,其中包含捕获的图像。

接下来,我想将图像添加到我使用照片框架在照片中创建的相册中。我可以毫无问题地创建相册,并且我已经验证了 PHAssetCollection 对象。我在第二种方法中使用的是正确的方法。

但是,出于某种原因,我无法将捕获的 UIImage 添加到相册中。我尝试将项目中的随机图像文件添加到相册中,操作成功完成。我还通过使用 NSLog 语句检查两种方法中的图像描述,验证了正确的图像已成功传递给 addPhotoToSavedPhotos 方法。这让我相信图像以某种方式出了问题,但 imageView 成功显示了它,所以我不确定那可能是什么。

如果有人对我可以尝试的解决方案有任何想法,我将不胜感激。此外,第二种方法中 NSLog 语句中的 error.localizedDescription 输出 "The operation couldn't be completed。(Cocoa 错误 -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);
}
}];
}

最佳答案

尝试这样的事情这段代码将首先检查相册是否存在

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

关于ios - 使用相机拍摄的 UIImage 的 PHAssetChangeRequest 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41584347/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com