標籤:
1、自訂相簿(相容 iOS7 iOS8)
-
- (void)viewDidLoad {//search all photo albums in the library
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
//compare the names of the albums
if ([@"九噠" compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
//target album is found
_isHaveAlbum = YES;
return;
}
if (group==nil && _isHaveAlbum == NO) {
//photo albums are over, target album does not exist, thus create it
//__weak ALAssetsLibrary* weakSelf = assetsLibrary;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
// iOS 8 code
// Create new album.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九噠"];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error creating album: %@", error);
}
}];
}
else {
// iOS 7.x code
//create new assets album
[assetsLibrary addAssetsGroupAlbumWithName:@"九噠" resultBlock:nil failureBlock: nil];
}
//should be the last iteration anyway, but just in case
_isHaveAlbum = YES;
return;
}
} failureBlock: nil];}
具體的解釋我已經在上一篇博文(http://www.cnblogs.com/cai-rd/p/4432908.html)中解釋, 這裡就不多解釋了, 上面代碼需要補充一個變數 isHaveAblum = NO;
2、把多張照片存入自訂的相簿中
-
for(int i = 0; i < 9; i++){// 利用延時每隔0.1秒存入 一次dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1*i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self saveImage:yourImage toAlbum:@"Rd" withCompletionBlock:^{ NSLog(@"save s img success"); }];});
}
/** * 儲存圖片到相簿中 * * @param image <#image description#> * @param albumName <#albumName description#> * @param completionBlock <#completionBlock description#> */-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(void (^)(void))completionBlock{ ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; //write the image data to the assets library (camera roll) [assetsLibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) { //error handling if (error!=nil) { //completionBlock(error); return; } //add the asset to the custom photo album [self addAssetURL: assetURL toAlbum:albumName withCompletionBlock:completionBlock]; }];}-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(void (^)(void))completionBlock{ ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; __block BOOL albumWasFound = NO; //search all photo albums in the library [assetsLibrary 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 [assetsLibrary assetForURL: assetURL resultBlock:^(ALAsset *asset) { //add photo to the target album [group addAsset: asset]; //run the completion block completionBlock(); } failureBlock: nil]; //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 __weak ALAssetsLibrary* weakSelf = assetsLibrary; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { // iOS 8 code // Create new album. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九噠"]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error creating album: %@", error); } }]; } else { // iOS 7.x code //create new assets album [assetsLibrary 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(); } failureBlock: nil]; } failureBlock: nil]; albumWasFound = YES; } //should be the last iteration anyway, but just in case return; } } failureBlock: nil];}
3、需要引入的標頭檔
ok~ 搞掂~ 有問題可以評論~ 一起探討~
iOS 開發: 自訂相簿, 儲存多張圖片到自訂相簿中