Closed fairzy closed 11 years ago
hi, i tried your code and it seems that if there is no album named as 'kKYCustomPhotoAlbumName_' defined before saving photos, just a few photos are saved, not all of them.
That's right, I tried & it gave me the same issue. I'll try to fix it later. Thanks! :)
As you said, when there's no photo album available, only part images are saved in a for loop. I tested the code and found that -addAssetsGroupAlbumWithName:resultBlock:failureBlock:
will process asynchrony, this method will be dispatched several times when the album is not created (this might take some time).
In this case, you can just create a photo album before image adding process:
[self.assetsLibrary addAssetsGroupAlbumWithName:kKYCustomPhotoAlbumName_
resultBlock:nil
failureBlock:nil];
Like the code below:
// manage tasks in background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// The completion block to be executed after image taking action process done
void (^completionBlock)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error) {
if (error) NSLog(@"!!!ERROR, write the image data to the assets library (camera roll): %@",
[error description]);
NSLog(@"*** URL %@ | %@ || type: %@ ***", assetURL, [assetURL absoluteString], [assetURL class]);
// Add new one to |photos_|
[self.photos addObject:[assetURL absoluteString]];
// Reload tableview data
[self.tableView reloadData];
};
void (^failureBlock)(NSError *) = ^(NSError *error) {
if (error == nil) return;
NSLog(@"!!!ERROR, failed to add the asset to the custom photo album: %@", [error description]);
};
// save image to custom photo album
if (! self.assetsLibrary) assetsLibrary_ = [[ALAssetsLibrary alloc] init];
// create a photo album before image adding process
[self.assetsLibrary addAssetsGroupAlbumWithName:kKYCustomPhotoAlbumName_
resultBlock:nil
failureBlock:nil];
// add images via a for loop
for (int i = 1; i <= 5; ++i)
[self.assetsLibrary saveImage:[UIImage imageNamed:[NSString stringWithFormat:@"Default-568h%d.png", i]]
toAlbum:kKYCustomPhotoAlbumName_
completionBlock:completionBlock
failureBlock:failureBlock];
});
ok, got it,,,
only the first image saved when trying to save several photos using for loop