iOS 将照片保存在特定应用的相册中 [英] iOS save photo in an app specific album
本文介绍了iOS 将照片保存在特定应用的相册中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个 iOS 5 应用程序.我想将照片保存到设备中.
I'm creating an iOS 5 app. I want to save a photo to the device.
我想将照片保存到特定于我的应用的相册中,因此我需要创建相册,然后将照片保存到相册中.
I want to save the photo to an album specific to my app, so I need to create the album, and then save photos to the album.
我知道如何创建相册:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"MY APP NAME" resultBlock:^(ALAssetsGroup *group) {
//How to get the album URL?
} failureBlock:^(NSError *error) {
//Handle the error
}];
我现在想将照片添加到新相册,我该怎么做?示例代码非常感谢!
I want add photos to the new album now, how do I do so? Sample code is greatly appreciated!
推荐答案
你可以使用下面的代码只是改变专辑的名字:
You may use the following code just change the name of album :
__weak ALAssetsLibrary *lib = self.library;
[self.library addAssetsGroupAlbumWithName:@"My Photo Album" resultBlock:^(ALAssetsGroup *group) {
///checks if group previously created
if(group == nil){
//enumerate albums
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
//if the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {
//save image
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
//then get the image asseturl
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
//put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
}failureBlock:^(NSError *error){
}];
}else{
// save image directly to library
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error) {
}];
这篇关于iOS 将照片保存在特定应用的相册中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文