我想裁剪一张 200*200 的图片,裁剪的部分是 (x=10,y=10,w=50,h=50)。并将这部分绘制到新的 500*500 图像上,新的矩形是 (x=30,y=30,w=50, h=50),怎么做?
我可以通过以下方法获取图像的一部分
- (UIImage*) getSubImageWithRect: (CGRect) rect {
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[self drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
Best Answer-推荐答案 strong>
让我们尝试使用以下代码块
- (UIImage*) getSubImageFromImageUIImage *)image
{
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(10, 10, 50, 50);
// Create Image Ref on Image
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
// Get Cropped Image
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
关于iOS如何将图像的一部分绘制到特定的矩形?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/33206077/
|