简体   繁体   English

iPhone:将图像保存到特定的相册

[英]iPhone: Save image to a specific photo album

The application I am working on manipulates an image for a user, and after the user is done, they can save that photo. 我正在使用的应用程序为用户处理图像,完成用户操作后,他们可以保存该照片。 I have no problem capturing the screen and saving it (as I want some of the labels saved with the image) but this call: 我捕获屏幕并保存它没有问题(因为我希望将某些标签与图像一起保存),但是此调用:

UIImageWriteToSavedPhotosAlbum([self getScreenAsImage] , nil, nil, nil); UIImageWriteToSavedPhotosAlbum([self getScreenAsImage],nil,nil,nil);

only appears to allow me to save to the 'Saved Photos' album. 只是为了允许我保存到“保存的照片”相册而出现。 Ideally I would like to create an album named after the application I am working on and save it there so that they are all stored together (for example like the Hawaii or graduation day albums on the simulator). 理想情况下,我想创建一个以我正在处理的应用程序命名的专辑,并将其保存在此处,以便将它们全部存储在一起(例如,像是夏威夷人或模拟器上的毕业典礼日专辑)。 It would also be nice then to launch the image picker in that specific album. 然后在该特定相册中启动图像选择器也将很不错。 Anyone know how to do this? 有人知道怎么做吗? Most important part is to be able to save it in an album, being able to launch the picker in that album is of secondary importance. 最重要的部分是能够将其保存在相册中,能够启动该相册中的选择器是次要的。

There is a way to do it, Apple just hasn't made it simple. 有一种方法可以做到,Apple并没有使其变得简单。

There is a custom category that adds the functionality to ALAssetsLibrary. 有一个自定义类别,将功能添加到ALAssetsLibrary。

Git repo for the category can be found here 可以在这里找到该类别的Git回购

and if you are using Cocoapods you can get it with 如果您正在使用Cocoapods,则可以使用

pod 'ALAssetsLibrary-CustomPhotoAlbum'

You will need to instantiate a ALAssetsLibrary object and call the method below 您将需要实例化ALAssetsLibrary对象并调用下面的方法

- (void)saveImage:(UIImage *)image
      toAlbum:(NSString *)albumName
   completion:(ALAssetsLibraryWriteImageCompletionBlock)completion
      failure:(ALAssetsLibraryAccessFailureBlock)failure

Something like below 像下面这样

Import: 进口:

#import "ALAssetsLibrary+CustomPhotoAlbum.h" //Source Included (Git)

or 要么

#import <ALAssetsLibrary+CustomPhotoAlbum.h> //Cocoapods

Call: 呼叫:

ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib saveImage:imageToSave
                      toAlbum:@"AlbumName"
                   completion:completionBlock
                      failure:failureBlock];

You can use the ALAssetsLibrary for doing this: 您可以使用ALAssetsLibrary执行此操作:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

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

Check this tutorial also: Custom Photo Album 还请检查本教程: 自定义相册

To the best of my knowledge Apple has not exposed an SDK to allow for this. 就我所知,Apple尚未公开允许使用此功能的SDK。 After a review the UIKit function reference, My fear seems confirmed. 查阅UIKit函数参考后,我的恐惧似乎得到了证实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM