首先,你需要創建一個文件夾:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:folderName
resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"Added folder:%@", folderName);
}
failureBlock:^(NSError *error)
{
NSLog(@"Error adding folder");
}];
然後,找到文件夾:
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:folderName])
{
folder = group;
}
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
,並添加您的照片吧。
保存圖片到素材資源庫,並把它放到專輯:
[library writeImageToSavedPhotosAlbum:yourImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (error.code == 0)
{
// Get the asset
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
// Assign the photo to the album
[folder addAsset:asset];
}
failureBlock:^(NSError* error)
{
// Error handling.
}];
}
else
{
// Error handling.
}
}];
謝謝!我不得不惹惱這一點,通過檢查文件夾是否已經添加過,因此我使用NSUserDefaults的BOOL,但這是要走的路。當你保存照片時,你的照片被錯誤地旋轉了嗎? – dokun1