1

已经有了答案,可以使用ALAssetsLibrary将图像保存到特定文件夹中。但它在 iOS 9.0 中已被弃用,并给出警告"Use PHPhotoLibrary from the Photos framework instead"

当我将 ALAssetsLibrary 更改为 PHPhotoLibrary 时,此时出现错误。

    [self.library saveImage:image toAlbum:@"myAlbum" withCompletionBlock:^(NSError *error) {
    if (error!=nil) {
        NSLog(@"Big error: %@", [error description]);
    }
}];

*错误

 No visible @interface for 'PHPhotoLibrary' declares the selector 'saveImage:toAlbum:withCompletionBlock:' 

如何使用PHPhotoLibrary(或)任何其他解决方案将图像保存到特定文件夹中。

先感谢您。

4

2 回答 2

3

我还没有使用过PHPhotoLibrary,但我知道使用本地文档目录在 Obj-C 中保存照片的老式方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData = UIImagePNGRepresentation(image);

NSString *imagePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,imageFolder];

BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
    if(![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:NULL])
        NSLog(@"Error: folder creation failed %@", documentsDirectory);

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, imageName] atomically:YES];

要从 NSDocumentDirectory 中检索该图像:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, imagePath]; // image path is same as above
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
    UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
    return image;
} else {
    return nil;
}
于 2015-11-09T07:28:16.033 回答
0

因为没有这样命名的方法。

要将图像保存到 PHPhotoLibrary,您需要创建一个资产。看

哪个 PHAssetCollection 用于保存图像?

了解详细信息。

于 2015-11-09T07:27:25.187 回答