我正在尝试使用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();
出于某种原因,最终的照片并不那么清晰,它有点模糊。我在这里做错了吗?任何指针都会非常感激。感谢
答案 0 :(得分:1)
我假设您正确计算矩形。然后确保使用整数矩形。非整数值可能导致子像素渲染。
运行projectRect
至CGRectIntegral
以获取整数矩形,然后使用它来渲染图像。
projectRect = CGRectIntegral(projectRect);