ios - 使用 ALAssetLibrary 保存到相册时失败

标签 ios uiimage alassetslibrary

我正在尝试使用 Marin Todorov 的 ALAssetsLibrary+CustomPhotoAlbum 将我生成的一些图像保存到使用 ALAssetLibrary 的相册中。但是,大多数时候我都会遇到随机故障。

  // Get the asset library instance.
  ALAssetsLibrary *library = [ALAssetLibraryManager defaultAssetsLibrary];
  [self.imageModels enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    // Sidenote: Trying to use an image that I have locally in my image assets fails as well.
    // UIImage *image = [UIImage imageNamed:@"Background-light-png.png"];
    UIImage *image = [UIImage imageWithContentsOfFile:[obj valueForKey:@"imagePath"]];
    [_imagesToShare addObject:image];
    if (image){
        [library saveImage:image toAlbum:@"My Album" completion:^(NSURL assetURL, NSError error) {
            NSLog(@"Success");
        } failure:^(NSError *error) {
            NSLog(@"Failed %@", error.description);
        }];
    }
}];

在失败 block 中,如果我添加以下代码,我可以看到图像已保存到相机胶卷中。

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

我得到的错误是

Failed: Error Domain=LIB_ALAssetsLibrary_CustomPhotoAlbum Code=0 "ALAssetsGroup failed to add asset." UserInfo=0x171277c80 {NSLocalizedDescription=ALAssetsGroup failed to add asset.}

获取资源库代码

+ (ALAssetsLibrary *)defaultAssetsLibrary {
   static dispatch_once_t pred = 0;
   static ALAssetsLibrary *library = nil;
   dispatch_once(&pred, ^{
        library = [[ALAssetsLibrary alloc] init];
   });
   return library;
}

我搜索了很多,但找不到关于这个主题的太多信息。我的方法是通过单击按钮调用的,并且不应同时发生其他写入。我在网上找到的唯一信息是您应该保留相同的 ALAssetLibrary 实例,我已经使用单例类完成了这一点。

最佳答案

这是我使用资源库保存图片的方式:

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:((UIImage *)[info objectForKey:UIImagePickerControllerEditedImage]).CGImage
                             metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
                      completionBlock:^(NSURL *assetURL, NSError *error) {
                          NSLog(@"assetURL %@", assetURL);
                            imageURL=[assetURL absoluteString];

                          [[NSUserDefaults standardUserDefaults] setObject:imageURL forKey:@"imageurl"]; //for reference of the path

                      }];

关于ios - 使用 ALAssetLibrary 保存到相册时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32503742/

相关文章:

ios - 针对 iPhone 6/iPhone 6 plus 优化应用

iphone - 捕获来自 Singleton 的更改

ios - 触摸时如何使图像扩展到全屏?

iphone - 用相机拍摄的照片不包含任何 ALAsset 元数据

iphone - ALAssetsLibrary ALAssetsLibraryDataUnavailableError

ios - 从 Google Analytics for iOS v2 获取特定的 iOS 设备名称

ios单元格突出显示颜色clipstobounds

ios - UIImagePickerController 允许我拍摄照片和视频,但不允许我保存它们或 "use"它们

iphone - 来自 YouTube 的缩略图

objective-c - 正确裁剪从照片库中获取的图像