缩放图像时的图像分辨率问题

时间:2013-10-30 07:42:22

标签: ios iphone uiimage screen-capture

在我的iPhone应用程序中,我必须进行图像编辑。用户可以在图像上添加标题,然后使用标题保存该图像并共享该图像。首先,当我们从图库中选择图片时,我使用了以下方法来缩放我的图像视图的图像:

 UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0f);
float hfactor = img.size.width / screenRect.size.width;
float vfactor = img.size.height / screenRect.size.height;

float factor = MAX(hfactor, vfactor);

float newWidth = img.size.width / factor;
float newHeight = img.size.height / factor;

float leftOffset = (screenRect.size.width - newWidth) / 2;
float topOffset = (screenRect.size.height - newHeight) / 2;

if (leftOffset>0) {
    mainImgView.frame=CGRectMake(leftOffset+10, screenRect.origin.y, newWidth, newHeight);
}
else if(topOffset>0)
{
    mainImgView.frame=CGRectMake(screenRect.origin.x, topOffset+10, newWidth, newHeight);
}

CGRect newRect = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);

[img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;

}

为了保存带有标题的图像,我已经截取了使用以下代码拍摄图像视图和标题标签的视图截图:

 UIGraphicsBeginImageContextWithOptions(mainImgView.frame.size,NO,0.0f);
 [mainImgView.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *newImage=[[UIImage alloc]init];
 newImage=UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

但是通过缩短屏幕截图,保存的图像分辨率非常低,并且在缩放图像时会得到像素化。有没有办法用实际分辨率保存图像,以便图片和标题都保存为图像?

1 个答案:

答案 0 :(得分:0)

可能原因在于视网膜显示的scaleFactor。请注意,在视网膜设备中,图像必须比容器的大小大两倍,才能保持它。 试试这个:

     UIGraphicsBeginImageContextWithOptions(mainImgView.frame.size,NO, [UIScreen     mainScreen].scale);