2

我正在使用以下代码将图像保存到在 iOS 照片库中创建的自定义文件夹中:

我收到此错误:Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

谁能帮我解决这个问题?

+(BOOL)saveToAlbum:(NSString*)albumName image:(UIImage*)image{
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
__block BOOL retStat = false;
NSData *newImageSize = UIImageJPEGRepresentation(image, 1);

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageWithData:newImageSize]];
    placeholder = [assetRequest placeholderForCreatedAsset];
    photosAsset = [PHAsset fetchAssetsInAssetCollection:[self findAlbumAssetCollection:albumName] options:nil];
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
                                                                                                                  assets:photosAsset];
    [albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success){
        retStat = true;
    }
    else{
        NSLog(@"%@", error);
    }}];
}
4

2 回答 2

3

在这里回答,以便有人可以在谷歌搜索结果中找到这个并节省我浪费的时间:如果您在创建资产时遇到此错误,请检查您的手机存储空间。我收到此错误是因为我的手机有 0 个字节(是的,0 个字节)可用存储空间,这是另一个无声错误的传播。

于 2016-01-21T17:59:39.743 回答
1

感谢 Mr.T 为我提供提示(albumChangeRequest 的收藏为零)..这是正确的解决方案。

+(BOOL)saveToAlbum:(NSString*)albumName image:(UIImage*)image{
__block BOOL retStat = false;
NSData *newImageSize = UIImageJPEGRepresentation(image, 1);
UIImage *imageNew = [UIImage imageWithData:newImageSize];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
     PHFetchResult *photosAsset;
     PHAssetCollection *collection = [self findAlbumAssetCollection:albumName];
     PHObjectPlaceholder *placeholder;
    placeholder = [[PHAssetChangeRequest creationRequestForAssetFromImage:imageNew] placeholderForCreatedAsset];
    photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset];                                                                       
    [albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success){
        retStat = true;
    }
    else{
        NSLog(@"%@", error);
    }}];
return  retStat;}


+(PHAssetCollection*)findAlbumAssetCollection:(NSString*)albumName{
PHAssetCollection *collection;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumName];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                      subtype:PHAssetCollectionSubtypeAny
                                                      options:fetchOptions].firstObject;
return collection;

}

于 2015-10-08T15:18:54.227 回答