如何从另一个图像的一部分创建图像? [英] How to create an image from part of another image?

查看:35
本文介绍了如何从另一个图像的一部分创建图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在 iOS swift 中从另一个图像创建一个图像?

How do you create an image from another image in iOS swift?

例如如果我有 1000 像素 x 1000 像素的图像 (A).我将如何从图像 (A) 的中间创建 200 像素 x 200 像素的图像 (B).

E.g. If I had image (A) that was 1000px by 1000px. How would I create image (B) a 200px by 200px from the middle of image (A).

推荐答案

您可以在 Core Graphics 中轻松完成此操作...

You can do this easily in Core Graphics...

func getSubImage(image:UIImage, subRect:CGRect) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(subRect.size, YES, 0);

    let ctx = UIGraphicsGetCurrentContext();

    let contextOrigin = CGPointMake(-subRect.origin.x, subRect.size.height+subRect.origin.y-image.size.height); // Translates coordinates into Core Graphics space

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -subRect.size.height);

    CGContextDrawImage(ctx, CGRectMake(contextOrigin.x, contextOrigin.y, image.size.width, image.size.height), image.CGImage);

    let img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}

这篇关于如何从另一个图像的一部分创建图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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