裁剪 UIImage 截图

Posted

技术标签:

【中文标题】裁剪 UIImage 截图【英文标题】:Cropping UIImage Screenshot 【发布时间】:2012-08-22 11:36:00 【问题描述】:

我目前正在替换我的代码中的 UIGetScreenImage(),因为它会导致 C99 错误。

我用this code from apple 替换它,screenshot 方法返回一个UIImage

我在裁剪和保存图像方面的尝试如下:

UIImage *returnedImage = [self screenshot];
// Get image to save
CGImageRef screen = returnedImage.CGImage;
UIImage *img = [UIImage imageWithCGImage:screen];

// Image cropping
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 5, 768, 1004));
UIImage *img2Save = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(img2Save, self, nil, nil);

在返回之前我也尝试在screenshot 方法中对其进行裁剪,但无济于事。

两次尝试都会保存从screenshot 方法返回的未裁剪的UIImage

有什么明显的我要遗漏的吗?我查看了this SO thread,但看不到连接。

【问题讨论】:

【参考方案1】:

请尝试以下代码

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect

   //create a context to do our clipping in
   UIGraphicsBeginImageContext(rect.size);
   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   //create a rect with the size we want to crop the image to
   //the X and Y here are zero so we start at the beginning of our
   //newly created context
   CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
   CGContextClipToRect( currentContext, clippedRect);

   //create a rect equivalent to the full size of the image
   //offset the rect by the X and Y we want to start the crop
   //from in order to cut off anything before them
   CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                rect.origin.y * -1,
                                imageToCrop.size.width,
                                imageToCrop.size.height);

   //draw the image to our clipped context using our offset rect
   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);

   //pull the image from our cropped context
   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

   //pop the context to get back to the default
   UIGraphicsEndImageContext();

   //Note: this is autoreleased
   return cropped;

然后用类似这样的方式调用代码:

- (void)drawRect:(CGRect)rect

   //draw the whole lights off image
   //the on images will be drawn overtop
   [lightsOffImage drawInRect:rect];

   //if we don't have any lights on... no point in continuing
   if( numberOfLightsOn < 1 )
     return;

   //figure out the dimensions of numberOfLights on bulbs
   CGSize croppedSize = CGSizeMake(LIGHT_WIDTH * numberOfLightsOn, LIGHT_HEIGHT);
   CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);

   //get the "on" bulbs by cropping the image
   UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];

   //create a rect to draw the newly cropped on images to
   CGRect lightsOnRect = CGRectMake(LIGHT_BULB_OFFSET_X,
                                    LIGHT_BULB_OFFSET_Y,
                                    croppedSize.width,
                                    croppedSize.height);

   //draw the "on" lights
   [cropped drawInRect:lightsOnRect];

   //cropped is autoreleased so no need to worry about cleanup

希望这段代码对你有所帮助。

【讨论】:

嗨@neon samuel,我曾尝试从另一个线程调用该函数。发生的情况是它似乎反转图片并缩小图像。这是由于我传递给它的坐标吗?我不认为它会反转,因为我传递给该方法的前 2 个参数是 0。

以上是关于裁剪 UIImage 截图的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIView 框架裁剪 UIImage

裁剪后 UIImage 旋转 90 度

通过自定义形状裁剪 UIImage

如何裁剪 UIImage?

UIImage 在裁剪后被旋转

裁剪的 UIImage 方向/拉伸