为什么图像在将视图转换为图像时变焦?

时间:2013-08-06 10:14:23

标签: iphone objective-c uiimageview

我有uiview,我希望将该视图转换为运行良好的图像但是当我按下iphone的主页按钮并再次运行应用程序时,图像将变焦。这是我将代码转换为图像的代码。

When the app 1st time run

After pressing the home button and run the app again.

- (UIImage *)captureView {
        CGRect rect = [myview bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [myview.layer renderInContext:context];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;

 }
 checkImage.image = [self captureView];

3 个答案:

答案 0 :(得分:1)

此处此代码没有错,但您使用了其他一些对象而不是整个视图。我不知道,但尝试使用整个图像显示的视图边界..从这个波纹链接看到我的答案,就像你的答案..

how to capture uiview top uiview

还可以在代码中使用self.view代替myView

添加此代码而不是checkImage.image = [self captureView];行..

 UIImageView *imgTempHeader = [[UIImageView alloc]initWithImage:[self captureView]];
 imgTempHeader.frame = self.view.frame;
 [self.view addSubview:imgTempHeader];
 [self.view bringSubviewToFront:imgTempHeader];

答案 1 :(得分:0)

您应该以使用设备规模的方式创建上下文:

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

然后,当您在图像视图中显示图像时,请确保将内容模式设置为阻止缩放。

答案 2 :(得分:0)

我的经验是在IB中设置imageViews。我假设checkImage是UIImageView的一个实例?

如果您在UIImageView中显示图像,并且只在界面构建器中将视图模式设置为居中,那么如果图像很大,它将会放大。

首先看一下处理图片的方式:

UIImage *newImage = [[UIImage alloc] initWithCGImage:myCGImageRef scale:myZoomSCale orientation:UIImageOrientationDown];

当然,您需要为您的图像提供CGImageRef。对你或其他人来说。