Saving multiple images to camera roll, only saved

2020-06-28 00:42发布

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 :)

4条回答
霸刀☆藐视天下
2楼-- · 2020-06-28 01:22

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.

查看更多
Fickle 薄情
3楼-- · 2020-06-28 01:35

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.

查看更多
不美不萌又怎样
4楼-- · 2020-06-28 01:41

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.

查看更多
▲ chillily
5楼-- · 2020-06-28 01:42

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];
查看更多
登录 后发表回答