将图像保存在库中

时间:2012-01-27 19:51:57

标签: iphone ios ipad uiimageview uiimage

我必须在iPhone和iPad的库中保存图像,所以这里是代码

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

但问题是我想用子视图保存图像意味着在UIImageView中有一些其他视图如UILabel和UIImageView所以如何保存带有所有子视图的图像。

屏幕截图是方式,但屏幕尺寸是固定的,但我想要用户捕获的图像的实际尺寸。

提前致谢。

2 个答案:

答案 0 :(得分:2)

如果你这样做也可以这样做

// Take a UIView of the image size
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,img.size.width,img.size.height)];
UIImageVIew *imgView = [[UIImageView alloc]initWithFrame:view.frame];
[imgView setImage:img];
[view addSubview:imgView];// after this give the positioning of your labels in the view and then perform this operation.

UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

此代码肯定会有效,但如果您无法提供正确的框架,则标签的位置将从您实际放置的位置移动。

答案 1 :(得分:0)

这样的事情:

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();