0

在此处输入图像描述

我有 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;

    }
4

2 回答 2

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;
}
于 2013-02-01T11:27:39.500 回答
0

更改此行

self.imgViewCurrent.contentMode = UIViewContentModeScaleToFill;

到这条线

 self.imgViewCurrent.contentMode = UIViewContentModeScaleAspectFit

UIViewContentModeScaleToFill基本上会拉伸图像。

于 2013-02-01T11:22:58.797 回答