Jacob
Jacob

Reputation: 1459

Saving multiple images to camera roll, only saved a few

Im attempting to save some images to a camera roll, all the images are in a array.

    if (buttonIndex == 1) {
    int done = 0;
    NSMutableArray *copyOfImages = [[NSMutableArray alloc]initWithArray:saveImagesToArray];
    while (!done == 1){

        if (copyOfImages.count == 0) {
            done = 1;
        }
        if (copyOfImages.count > 0){
            [copyOfImages removeLastObject];
        }

    UIImage *image = [[UIImage alloc]init];
    image = [copyOfImages lastObject];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    }

}

because i dont know how many images there can be i use a while loop

Im testing this with 8 images, the array shows a count of 8 so thats good.

When i try saving the images this comes up in the console.

-[NSKeyedUnarchiver initForReadingWithData:]: data is NULL

Out of the 8 images im trying, only 5 show up in the camera roll.

Any help would be great. Thanks :)

Upvotes: 2

Views: 4360

Answers (4)

Rankito
Rankito

Reputation: 23

I had the same exact problem. No matter how much pictures i added to the share activityController, maximum of 5 were saved.

i have found that the problem was to send the real UIImage and not URL:

NSMutableArray *activityItems = [[NSMutableArray alloc] init];

for(UIImage *image in imagesArray) {
    [activityItems addObject:image];
}

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

Upvotes: 1

toblerpwn
toblerpwn

Reputation: 5545

To expand on Bill's answer, UIImageWriteToSavedPhotosAlbum seems to do its writing on some unknown thread asynchronously - but also there is some hidden limit to the number of images it can write at once. You can even tease out write busy-type errors if you dig in deep.

Tons more info here:

Objective-C: UIImageWriteToSavedPhotosAlbum() + asynchronous = problems

I also agree with Bill that serializing your writes is the only reasonable/reliable answer I have seen.

Upvotes: 0

Bill
Bill

Reputation: 45408

I had this same issue and I resolved it by ensuring that the images are saved sequentially. I think there may be some kind of race condition going on.

Basically, I did:

 UIImageWriteToSavedPhotosAlbum([self.images objectAtIndex:self.currentIndex], self, 
   @selector(image:didFinishSavingWithError:contextInfo:), nil);

Then in image:didFinishSavingWithError:contextInfo:, I increment currentIndex and try the next image, if there are any left.

This has worked for me in every case so far.

Upvotes: 1

sosborn
sosborn

Reputation: 14694

Why are you calling [copyOfImages removeLastObject]? Every time you go through that look are you destroying the last object, which is strange because you haven't added it to the roll yet. Take out that line and see if you look works.

Also, rather than using a for loop, use the following pattern:

for (id object in array) {
    // do something with object
}

This will enumerate though the objects in the array, stopping when it reaches the end. Just be sure not to modify the array while you are doing this.

Upvotes: 1

Related Questions