27 AugSave multiple wallpapers to Photos Album, UIImageWriteToSavedPhotosAlbum

Sure you know about method to copy an image to user photo library:

void UIImageWriteToSavedPhotosAlbum(UIImage
*image, id completionTarget, SEL completionSelector,
void *contextInfo);

but if you try to save multiple files at once

for (int i=1; i<=10; ++i){  UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}

you’ll have every second/third file saved or even nothing will be saved. To investigate what’s wrong it’s required to set complete selector to UIImageWriteToSavedPhotosAlbum:

- (void)image:(UIImage *) image didFinishSavingWithError: (NSError *)error
contextInfo:(void *)contextInfo;

looks like this:

for (int i=1; i<=10; ++i){ UIImageWriteToSavedPhotosAlbum(image,
self, @selector(savedPhotoImage : didFinishSavingWithError:contextInfo:), nil);} -
(void) savedPhotoImage:(UIImage*)image
didFinishSavingWithError:(NSError *)error contextInfo:
(void *)contextInfo{ NSLog(@"%@",
[error localizedDescription]);}

and then you’ll see an error description “write busy” at the log. System just unable to copy files so fast. Yes, you use just a mobile device with limited, but miraculous abilities ;) It isn’t 12 Core Mac Pro.

The solution is to copy photos one by one:

  1. create an array of image names, e.g. wallpapers
  2. copy one
  3. catch complete selector
  4. goto #2

- (void) saveWallpapers{ // fill array of names wallpapers = [[NSMutableArray alloc]
 initWithCapacity:10]; for (int i=1; i<=10; ++i) {  [wallpapers addObject:imageName];
}  // start copying [self saveNextWallpaper];} - (void) saveNextWallpaper{
// copy a wallpaper and remove from the array if (wallpapers && wallpapers.count > 0)
{  UIImage *image = [UIImage imageNamed:[wallpapers lastObject]];
UIImageWriteToSavedPhotosAlbum(image,
self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
[wallpapers removeLastObject]; }} - (void) savedPhotoImage:(UIImage*)image
didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo{
// if still any wallpaper if (wallpapers)
{  [self saveNextWallpaper]if (wallpapers.count == 0)
{   // finish copying   UIAlertView  *errorAlert = [[UIAlertView alloc]
nitWithTitle:nil            message:@"Wallpapers are stored to Photos Album"
delegate:nil            cancelButtonTitle:@"ОК"
otherButtonTitles:nil];   [errorAlert show];   [errorAlert release];
[wallpapers release],
wallpapers = nil;  } }  NSLog(@"%@", [error localizedDescription]);}

One Response to “Save multiple wallpapers to Photos Album, UIImageWriteToSavedPhotosAlbum”

  1. This post has been removed by a blog administrator.

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment


+ 7 = eleven