0

在我的应用程序中,我使用上传图像功能,如果图像具有或多或少的 DPI,我想将上传的图像转换为 72 DPI。

我们可以用cgimagecreate

请给我建议。

谢谢。

4

1 回答 1

0

我们可以通过以下方法获得 72 DPI 的图像,实际上我们只需要从上下文中获取新图像,如下所示。

-(UIImage *)resizeImageFor72DPI:(UIImage*)image newSize:(CGSize)newSize
{
CGRect newRect= CGRectZero;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]
    && [[UIScreen mainScreen] scale] == 2.0)
{
    //For retina image
    newSize=CGSizeMake(newSize.width/2.0,newSize.height/2.0);
}
newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));

CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;
}
于 2013-08-19T13:20:57.450 回答