2016-01-20 72 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; 

} 
相關問題