使用 ALAssetLibrary 保存到相册时失败

Failure when saving to album using ALAssetLibrary

我正在尝试使用 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);
        }];
    }
}];

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

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 实例,我使用单例 class.

我是这样用资源库保存图片的:

 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

                      }];