gpt4 book ai didi

iOS如何将图像的一部分绘制到特定的矩形?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:03 26 4
gpt4 key购买 nike

我想裁剪一张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;
}

最佳答案

让我们尝试使用以下代码块

- (UIImage*) getSubImageFromImage:(UIImage *)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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com