将多张图像保存到相机胶卷中,仅保存了几张

发布于 2024-12-13 00:04:59 字数 796 浏览 0 评论 0原文

我试图将一些图像保存到相机胶卷中,所有图像都在一个数组中。

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

    }

}

因为我不知道可以有多少图像,所以我使用了 while 循环,

我用 8 个图像进行了测试,数组显示的计数为 8,所以很好。

当我尝试保存图像时,它会出现在控制台中。

-[NSKeyedUnarchiver initForReadingWithData:]: data is NULL

在我尝试的 8 张图像中,只有 5 张出现在相机胶卷中。

任何帮助都会很棒。 谢谢 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梨涡少年 2024-12-20 00:04:59

为什么要调用[copyOfImages removeLastObject]
每次你经历那个外观时,你都会破坏最后一个物体,这很奇怪,因为你还没有将它添加到卷中。把那条线拿出来看看你看起来是否有效。

另外,不要使用 for 循环,而是使用以下模式:

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

这将枚举数组中的对象,并在到达末尾时停止。执行此操作时请确保不要修改数组。

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.

高速公鹿 2024-12-20 00:04:59

我遇到了同样的问题,我通过确保按顺序保存图像来解决它。我认为可能存在某种竞争条件。

基本上,我做了:

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

然后在 image:didFinishSavingWithError:contextInfo: 中,我增加 currentIndex 并尝试下一个图像(如果还有剩余)。

到目前为止,这对我在所有情况下都有效。

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.

七秒鱼° 2024-12-20 00:04:59

我有同样的问题。无论我向共享 ActivityController 添加多少图片,最多只能保存 5 张。

我发现问题是发送真实的 UIImage 而不是 URL:

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

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

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

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];
对岸观火 2024-12-20 00:04:59

为了扩展 Bill 的答案,UIImageWriteToSavedPhotosAlbum 似乎在某个未知线程上异步写入 - 但它一次可以写入的图像数量也存在一些隐藏的限制。如果深入挖掘,您甚至可以找出 write busy 类型的错误。

这里有更多信息:

Objective-C: UIImageWriteToSavedPhotosAlbum() + asynchronous = issues

我也同意比尔的观点,序列化你的写入是唯一合理/可靠的答案
我见过。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文