【问题标题】:How to create an image from part of another image?如何从另一个图像的一部分创建图像?
【发布时间】:2016-01-29 01:30:31
【问题描述】:

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

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

【问题讨论】:

标签: ios swift uiimage


【解决方案1】:

您可以在 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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2014-10-16
    • 1970-01-01
    • 2012-09-18
    • 2013-09-23
    • 2012-05-10
    • 2018-11-24
    相关资源
    最近更新 更多