在我的 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();
但是通过缩短屏幕,保存的图像分辨率非常低,并且在我们缩放该图像时会像素化。有没有办法以实际分辨率保存图像,以便图片和标题都保存为图像?