裁剪后,图像变得紧张和模糊 [英] Image is getting stretched and blur after crop

查看:158
本文介绍了裁剪后,图像变得紧张和模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是拥有和UIImageView,UIImageView上还有另一个画布视图。我想根据画布视图的框架剪切UIImageView的图像。但是裁剪完成后,图像会变得紧张并且在裁剪后变得模糊。以下是我的代码。

I am having and UIImageView and there is another canvas view over the UIImageView. I want to cut the UIImageView's image according to the canvas view's frame. But after crop the image is getting stretched and blur after crop. Below are my codes.

[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;

    }


推荐答案

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

After cropping your image, you can resize your image with Custom Size. It may help you.

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

这篇关于裁剪后,图像变得紧张和模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆