您的位置:首页 > 其它

保存图片到IPhone图片库中

2013-06-30 12:52 155 查看
有时候需要将应用中的图片保存到用户iPhone或者iTouch的相册中。 可以使用UIKit的这个类方法来完成。

void UIImageWriteToSavedPhotosAlbum (
UIImage *image,   // 要保存到用户设备中的图片
id completionTarget,  //可选的参数,当保存完成后,回调方法所在的对象
SEL completionSelector,  //可选的参数,当保存完成后,所调用的回调方法。
void *contextInfo  //可选的参数,保存了一个指向context数据的指针,它将传递给回调方法。
);


比如可以这样来写一个存储图片的方法:

// 要保存的图片
UIImage *img = [UIImage imageNamed:@"ImageName.png"];
// 保存图片到相册中
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
可以在保存图片的时候加个动画:
CATransition* animation = [CATransition animation];
[animation setType:@"suckEffect"];
[animation setDuration:0.5f];
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
//self.view.opaque = 1.0f;
[self.view.layer addAnimation:animation forKey:@"animationEffect"];
[self.view.layer removeAnimationForKey:@"animationEffect"];


回调方法:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
//错误处理
if (error != NULL) {
// Show error message...
}
else { //保存图片成功
// Show message image successfully saved
}
}


截取屏幕图片可以这样:

-(UIImage *)imageFromView:(UIView *)theView {
UIGraphicsBeginImageContext(CGSizeMake(320, theView.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: