将图像保存到照片库,但有时无法正常工作 [英] save images to photo library, but sometimes can not work correctly
问题描述
我使用下面的代码将图像保存到照片相册中
I used the codes below to save images to photo librabry
for(int i=0;i<[imageNameArray count];i++)
{
NSMutableString *sss=[[NSMutableString alloc] initWithString: myAppPath];
[sss appendString:@"/"] ;
[sss appendString: [NSString stringWithFormat:@"%@",[imageNameArray objectAtIndex:i] ] ] ;
UIImage *image = [[[UIImage alloc] initWithContentsOfFile:sss]autorelease];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[sss release];
}
但是有时它不能正确保存图像,我发现在照片库中有一个或多个黑色块以缩略图模式替换了保存的图像,但是如果单击黑色块,则原始图像可以正确显示.
but sometimes, it can not save images correctly, I found that in photo library, there are one or more black blocks replace the saved images in thumbnail mode, but if I click the black blocks, the original images can display correctly.
有人遇到过同样的问题吗?
Is there anyone met the same problem?
欢迎发表评论
谢谢
interdev
推荐答案
快速连续保存图像可能会导致问题,因此保存调用重叠.(将映像写入磁盘要比从函数返回返回磁盘花费更多的时间.)
It could be a problem that you're saving the images in quick succession, and as a result, the save calls are overlapping. (It takes longer to write an image to the disk than it does to return from the function).
据我了解,保存图像功能是异步的,并且允许注册委托/回叫(在您传递nil的地方).
As far as I understand the save image function is asynchronous, and allows a delegate/call back to be registered (in the places where you're passing nil).
您可能应该等到一个图像完成写入后再开始其他图像.
You should probably wait until one image has finished writing until you start another.
您可以使用委托和回调来设计它.为了使用户更好,并让他们了解发生了什么,您可以显示进度条和其他信息,例如保存的1/3图像"等.
You can engineer this using the delegate and callbacks. To make it better for the user and to give them an idea of whats happening, you can display a progress bar and other information such as "1/3 images saved" etc.
为了获得更好的上下文,我查阅了文档:
For a better context, I looked up the documentation:
UIImageWriteToSavedPhotosAlbum
函数的声明如下:
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
您应该设置 completionTarget
(可能是 self
)和 completionSelector
,它们应具有以下签名:
you should set the completionTarget
(likely to be self
) and the completionSelector
which should have the following signature:
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo;
在完成选择器中,您可以执行是否需要执行另一次保存并更新进度" UI的逻辑.
In the completion selector, you can perform the logic of whether or not you need to perform another save, and update your 'progress' UI.
这篇关于将图像保存到照片库,但有时无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!