在循环语句中批量保存图片到相册时会有丢失的情况,代码一般是这个样子
for (int i = 0; i < n; i++) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
debug了一下,错误是
write busy
原因
iOS 往系统相册写照片的时候是单线程,一张存完才会存下一张,可能是因为要经过这几个过程:压缩图片、生成缩略图、SQLite保存数据,低配置的机器(比如 iPhone4)有点慢,同时写入照片会有失败的情况
解决方法
知道原因后就好解决了,方法就是一张存成功再存下一张
先保存成数组
#pragma mark -下载
- (void)downLoadButtonClick:(UIButton*)button
{
[self saveImage];
}
#pragma mark -点击下载按钮
- (void)saveImage
{
for (int i = 0; i < n; i++) {
UIImage *image = ...;
[self.photosArray addObject:image];
}
[self saveNext];
}
//循环保存图片
- (void)saveNext
{
if(self.photosArray.count>0) {
UIImage*image = self.photosArray[0];
UIImageWriteToSavedPhotosAlbum(image,self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),nil);
}
else
{
[self allDone:JYPhotoBrowserSaveImageSuccessText];
}
}
-(void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo: (void*)contextInfo {
if(error) {
NSLog(@"%@", error.localizedDescription);
[self allDone:JYPhotoBrowserSaveImageFailText];
}
else{
[self.photoKeyArrayremoveObjectAtIndex:0];
}
[selfsaveNext];
}
//提示是否保存图片成功或者失败
- (void)allDone:(NSString*)prompt
{
UILabel*label = [[UILabelalloc]init];
label.textColor=FRONT_VIEW_Color;
label.backgroundColor= [UIColorcolorWithRed:0.1fgreen:0.1fblue:0.1falpha:0.90f];
label.layer.cornerRadius=HNYFrom6(5);
label.clipsToBounds=YES;
label.bounds=CGRectMake(0,0,HNXFrom6(150),HNYFrom6(30));
label.center=self.view.center;
label.textAlignment=NSTextAlignmentCenter;
label.font= [UIFontboldSystemFontOfSize:17];
[[UIApplicationsharedApplication].keyWindowaddSubview:label];
[[UIApplicationsharedApplication].keyWindowbringSubviewToFront:label];
label.text= prompt;
//[self.indicatorView removeFromSuperview];
[label performSelector:@selector(removeFromSuperview)withObject:nilafterDelay:1.0];
}
网友评论