2016-01-20 70 views
0

如何在iOS Swift中从其他图像创建图像?如何从另一幅图像的一部分创建图像?

E.g.如果我的图片(A)是1000px×1000px。我如何从图像(A)的中间创建200像素(200像素)的图像(B)。

+0

你可以在http://stackoverflow.com/a/28513086/1271826使用'imageByCroppingToBounds'。 – Rob

回答

0

可以核芯显卡做到这一点很容易...

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; 

} 
相关问题