裁剪后图像越来越模糊

在这里输入图像说明

我有和UIImageView和UIImageView有另一个canvas视图。 我想根据canvas视图的框架来裁剪UIImageView的图像。 但在收获后,图像在收割后变得越来越模糊。 以下是我的代码。

[UIImage *images = [self captureScreenInRect1:canvas.frame]; self.imgViewCurrent.contentMode = UIViewContentModeScaleToFill; self.imgViewCurrent.image = images; - (UIImage *)captureScreenInRect1:(CGRect)captureFrame { CALayer *layer; layer = self.view.layer;enter image description here UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0); CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame); [layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = CGImageCreateWithImageInRect([screenImage CGImage], captureFrame); UIImage *img = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:UIImageOrientationUp]; CGImageRelease(imageRef); return img; } 

改变这一行

 self.imgViewCurrent.contentMode = UIViewContentModeScaleToFill; 

到这一行

  self.imgViewCurrent.contentMode = UIViewContentModeScaleAspectFit 

UIViewContentModeScaleToFill将基本上延伸图像。

修剪完图像后,可以使用“自定义大小”调整图像大小。 它可以帮助你。

 -(UIImage*) resizedImage:(UIImage *)inImage: (CGRect) thumbRect { CGImageRef imageRef = [inImage CGImage]; CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate // see Supported Pixel Formats in the Quartz 2D Programming Guide // Creating a Bitmap Graphics Context section // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst, // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported // The images on input here are likely to be png or jpeg files if (alphaInfo == kCGImageAlphaNone) alphaInfo = kCGImageAlphaNoneSkipLast; // Build a bitmap context that's the size of the thumbRect CGContextRef bitmap = CGBitmapContextCreate( NULL, thumbRect.size.width, // width thumbRect.size.height, // height CGImageGetBitsPerComponent(imageRef), // really needs to always be 8 4 * thumbRect.size.width, // rowbytes CGImageGetColorSpace(imageRef), alphaInfo ); // Draw into the context, this scales the image CGContextDrawImage(bitmap, thumbRect, imageRef); // Get an image from the context and a UIImage CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage* result = [UIImage imageWithCGImage:ref]; CGContextRelease(bitmap); // ok if NULL CGImageRelease(ref); return result; }