• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:43:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想裁剪一张 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-推荐答案


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

- (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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap