使用 phphotolibrary 将图像保存到自定义图库文件夹 - iOS
Save image to custom galary folder using phphotolibraty - iOS
我正在使用以下代码将图像保存到我在 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);
}}];
}
感谢 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;
}
在这里回答,以便有人可以在 google 搜索结果中找到它并节省我浪费的时间:如果您在创建资产时遇到此错误,检查您的 phone 存储。我得到这个错误作为另一个静默错误的传播,因为我的 phone 有 0 个字节(是的,0 个字节)可用存储 space.
我正在使用以下代码将图像保存到我在 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);
}}];
}
感谢 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;
}
在这里回答,以便有人可以在 google 搜索结果中找到它并节省我浪费的时间:如果您在创建资产时遇到此错误,检查您的 phone 存储。我得到这个错误作为另一个静默错误的传播,因为我的 phone 有 0 个字节(是的,0 个字节)可用存储 space.