使用 UIGraphics 调整照片大小但最终图像有点模糊
Resizing a photograph using UIGraphics but final image is slightly blurry
我正在尝试使用 UIGraphics 调整图像的大小。图片是用相机拍的,我用的是这个代码:
CGSize origImageSize = photograph.size;
//this saves as 140*140 for retina
CGRect newRect = CGRectMake(0, 0, 70, 70);
//scaling ratio
float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
CGRect projectRect;
projectRect.size.width= ratio*origImageSize.width;
projectRect.size.height=ratio*origImageSize.height;
//center the image
projectRect.origin.x= ((newRect.size.width-projectRect.size.width)/2);
projectRect.origin.y=((newRect.size.height-projectRect.size.height)/2);
[photograph drawInRect:projectRect];
//get the image from the image context
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
由于某种原因,最终照片不是那么清晰,有点模糊。我在这里做错了什么吗?任何指针将不胜感激。谢谢
我假设您正确计算了矩形。然后确保使用完整的矩形。非整数值可能会导致亚像素渲染。
运行 你的 projectRect
通过 CGRectIntegral
得到完整的矩形,然后用它来渲染你的图像。
projectRect = CGRectIntegral(projectRect);
我正在尝试使用 UIGraphics 调整图像的大小。图片是用相机拍的,我用的是这个代码:
CGSize origImageSize = photograph.size;
//this saves as 140*140 for retina
CGRect newRect = CGRectMake(0, 0, 70, 70);
//scaling ratio
float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
CGRect projectRect;
projectRect.size.width= ratio*origImageSize.width;
projectRect.size.height=ratio*origImageSize.height;
//center the image
projectRect.origin.x= ((newRect.size.width-projectRect.size.width)/2);
projectRect.origin.y=((newRect.size.height-projectRect.size.height)/2);
[photograph drawInRect:projectRect];
//get the image from the image context
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
由于某种原因,最终照片不是那么清晰,有点模糊。我在这里做错了什么吗?任何指针将不胜感激。谢谢
我假设您正确计算了矩形。然后确保使用完整的矩形。非整数值可能会导致亚像素渲染。
运行 你的 projectRect
通过 CGRectIntegral
得到完整的矩形,然后用它来渲染你的图像。
projectRect = CGRectIntegral(projectRect);