给定图像,将白色添加到区域内的所有周围区域 - iOS

Cod*_*Guy -1 objective-c uiimage ios

给定尺寸为wx 的图像G h,如何用白色环绕图像以产生320 x 480的新图像,其中G居中,被白色包围.传入图像的宽度和高度可能会大于320或480,因此首先我需要对其进行缩放以确保它小于320x480(或等于).

-(UIImage *)whiteVersion:(UIImage *)img {

}
Run Code Online (Sandbox Code Playgroud)

我会使用这些步骤:

Firstly, scale image if needed...then:
1. Create a UIImage of size 320x480
2. Paint the whole thing white
3. Paint G (the image) in the center by using some simple math to find the location
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚第1步或第2步.

rob*_*off 5

你不画画.你绘制图形上下文.有不同种类的图形上下文.图像图形上下文允许您将其内容的快照作为图像.

- (UIImage *)whiteVersion:(UIImage *)myImage {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), YES, myImage.scale);
    [[UIColor whiteColor] setFill];
    UIRectFill(CGRectInfinite);
    [myImage drawAtPoint:CGPointMake((320 - myImage.size.width) / 2, (480 - myImage.size.height) / 2)];
    UIImage *myPaddedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return myPaddedImage;
}
Run Code Online (Sandbox Code Playgroud)

您可以通过阅读iOS 绘图和打印指南以及Quartz 2D编程指南,了解有关在iOS上绘图的更多信息.