您的位置:首页 > 其它

将图片写入相册

2015-10-27 11:20 225 查看
//图片在桌面上,获取原图片
for (int i = 0; i<9; i++) {
NSString *file = [NSString stringWithFormat:@"/Users/aplle/Desktop/images/%d.jpg", i];

UIImage *image = [UIImage imageWithContentsOfFile:file];
//写入相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}


UIImageView*imageVc = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
imageVc.frame = CGRectMake(100, 100, 100, 100);
imageView = imageVc;
[self.view addSubview:imageView];

UIButton*saveBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
saveBtn.frame = CGRectMake(10, 10, 20, 20);
[saveBtn addTarget:self action:@selector(didClickCollectionButtonAction:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:saveBtn];


//存储相册到本地
- (void)didClickCollectionButtonAction:(UIButton *)button{
//直接存入本地相册可以用:
UIImageWriteToSavedPhotosAlbum(imageView.image,nil,nil, nil);
//需要回调方法或者检验是否存入成功:
UIImageWriteToSavedPhotosAlbum(imageView.image,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}
//检查是否存储成功
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
//    if (!error) NSLog(@"保存成功");
//    else  NSLog(@"保存失败");

if (error !=NULL) {
UIAlertView *photoSave = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%@",error]delegate:nil cancelButtonTitle:nil otherButtonTitles:nil ];
[photoSave show];
[photoSave dismissWithClickedButtonIndex:0 animated:YES];
photoSave =nil;
}else {
UIAlertView *photoSave = [[UIAlertView alloc]initWithTitle:@"\n\n保存成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[photoSave show];
[photoSave dismissWithClickedButtonIndex:0 animated:YES];
photoSave =nil;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: