在IOS上,如果用ALAssertLibrary创建相册,如果该相册被删除过了,则再次创建,会创建成功,但是group为nil
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
//get the photo's instance
[weakSelf assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the newly created album
[group addAsset: asset];
//call the completion block
completionBlock(nil);
} failureBlock: completionBlock];
} failureBlock: completionBlock];
在苹果的官方文档中,有一段说明,大概意思就是IOS8之后使用了新的库PHPhotoLibrary代替ALAssetsLibrary。
导入库Photo.framework,此时注意修改后面的选项为option,否则你的应用就必须支持IOS8以及以上了。
IOS8 创建相册
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
} completionHandler:^(BOOL success, NSError *error) {
} failureBlock:completionBlock];
}
}];
//下面是修改ALAssetsLibrary+CustomPhotoAlbum 的代码,兼容IOS8,在外调用依旧不变
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;
//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
//target album is found
albumWasFound = YES;
//get a hold of the photo's asset instance
[self assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the target album
[group addAsset: asset];
//run the completion block
completionBlock(nil);
} failureBlock: completionBlock];
//album was found, bail out of the method
return;
}
if (group==nil && albumWasFound==NO) {
//photo albums are over, target album does not exist, thus create it
/*
(在 iOS 8.0 后, 使用the Photos framework 代替 the Assets Library framework ,
The Photos framework 提供更特色和更好的表现 在使用 photo library 工作的时候)
iOS 8.0 之后, 相册新增了 Recently Deleted (最近删除) 这个功能, 导致我们在相册里边手动删除 "Rd"相册 是并没有完全删除, 导致创建相册虽然成功, 但却为 nil ,
因为创建的相册还是处于 Recently Deleted 的状态;
*/
if (IS_IOS_8) {
__weak ALAssetsLibrary* weakSelf = self;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
[weakSelf enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
[weakSelf assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the target album
[group addAsset: asset];
//run the completion block
[self performSelectorOnMainThread:@selector(saveCompletionSuccess:) withObject:completionBlock waitUntilDone:YES];
} failureBlock: completionBlock];
}
} failureBlock:completionBlock];
}
}];
}else{
__weak ALAssetsLibrary* weakSelf = self;
//create new assets album
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
//get the photo's instance
[weakSelf assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the newly created album
[group addAsset: asset];
//call the completion block
completionBlock(nil);
} failureBlock: completionBlock];
} failureBlock: completionBlock];
//should be the last iteration anyway, but just in case
return;
}
}
} failureBlock: completionBlock];
}
原文出处:http://blog.csdn.net/wangchenggggdn/article/details/46532931