核心图形-如何从UIImage中裁剪非透明像素? [英] Core Graphics - how to crop non-transparent pixels out of a UIImage?

查看:106
本文介绍了核心图形-如何从UIImage中裁剪非透明像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImage正在从透明PNG(500px x 500px)读取。在图像中的某处,有一张我想要裁剪并另存为单独的UIImage的图片。我还想根据新裁剪的矩形的左侧和顶部的透明像素存储X和Y坐标。

I have a UIImage that is reading from a transparent PNG (500px by 500px). Somewhere in the image, there is a picture that I want to crop out and save as a separate UIImage. I also want to store the X and Y coordinates based on how many transparent pixels there were on the left and top of the newly cropped rectangle.

我能够裁剪具有以下代码的图像:

I was able to crop an image with this code:

- (UIImage *)cropImage:(UIImage *)image atRect:(CGRect)rect
{
    double scale = image.scale;
    CGRect scaledRect = CGRectMake(rect.origin.x*scale,rect.origin.y*scale,rect.size.width*scale,rect.size.height*scale);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], scaledRect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:scale orientation:image.imageOrientation];
    CGImageRelease(imageRef);
    return cropped;
}

实际上切断了顶部和左侧的透明像素:S(这是如果我也能够裁剪左右两侧的像素,那将是很好!)。然后,它将其余图像的大小调整为我指定的矩形。不幸的是,尽管我需要裁切位于图像中间的图片,并且需要使尺寸能够动态变化。

Which actually cuts off the transparent pixels on the top and left :S (this would be great if I was able to crop the pixels on right and bottom too!). It then resizes the rest of the image to the rectangle I specified. Unfortunately though I need to cut a picture that is in the middle of the image and I need the size to be able to be dynamic.

不得不为此奋斗了几个小时现在。有想法吗?

Been struggling with this for several hours now. Any ideas?

推荐答案

要裁剪图像,请将其绘制到较小的图形上下文中。

To crop an image, draw it into a smaller graphics context.

例如,假设您有一个600x600的图片。假设您要在所有四个侧面上裁剪200个像素。这样就留下了200x200的矩形。

For example, let's say you have a 600x600 image. And let's say that you want to crop 200 pixels off all four sides. That leaves a 200x200 rectangle.

因此,您可以使用 UIGraphicsBeginImageContextWithOptions 来制作200x200的图形上下文。然后,您可以使用 drawAtPoint:将图像绘制到其中,在点(-200,-200)处绘制。如果您考虑一下,您会发现该偏移量导致从原始中间位置开始的200x200被绘制到上下文的实际边界中。因此,您已经在所有四个面上裁剪了200像素的图像,这就是我们想要做的。

So you would make a 200x200 graphics context, using UIGraphicsBeginImageContextWithOptions. Then you would draw the image into it using drawAtPoint:, drawing at the point (-200,-200). If you think about it, you will see that that offset causes just the 200x200 from the middle of the original to be drawn into the actual bounds of the context. Thus you have cropped the image by 200 pixels on all four sides, which is what we wanted to do.

因此,这里是一个广义版本,假设我们知道该数量从左,右,上和下进行裁剪:

Thus here is a generalized version, assuming that we know the amount to crop from the left, right, top, and bottom:

UIImage* original = [UIImage imageNamed:@"original.png"];
CGSize sz = [original size];
CGFloat cropLeft = ...;
CGFloat cropRight = ...;
CGFloat cropTop = ...;
CGFloat cropBottom = ...;
UIGraphicsBeginImageContextWithOptions(
    CGSizeMake(sz.width - cropLeft - cropRight, sz.height - cropTop - cropBottom), 
    NO, 0);
[original drawAtPoint:CGPointMake(-cropLeft, -cropTop)];
UIImage* cropped = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

之后,已裁剪是裁剪后的图像

这篇关于核心图形-如何从UIImage中裁剪非透明像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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