裁剪后,图像变得紧张和模糊

时间:2013-02-01 11:19:09

标签: iphone ios ipad

enter image description here

我有UIImageView和UIImageView上有另一个画布视图。我想根据画布视图的框架剪切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;

    }

2 个答案:

答案 0 :(得分:0)

更改此行

self.imgViewCurrent.contentMode = UIViewContentModeScaleToFill;

到这一行

 self.imgViewCurrent.contentMode = UIViewContentModeScaleAspectFit

UIViewContentModeScaleToFill基本上会拉伸图像。

答案 1 :(得分:0)

裁剪图像后,您可以使用自定义尺寸调整图像大小。它可能会帮助你。

-(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;
}