如果 UIView 的大小高于您拥有的最大图像的大小,那么您正在做的保存 UIView 是一个非常好的主意,但几乎从来没有这种情况。
您需要做的是创建一个图形上下文 (CGContextRef),与最大的图像一样大,按 z 顺序对图像进行排序并开始在那里绘制这些图像。
我会尝试用一些伪代码来帮助你:
-(UIImage *)mergeFlag{
UIImage * mergedImage = nil;
//In some pace you need to fill these values with the biggest width of all your images and the biggest height;
int Max_Width;
int Max_Height;
//Use here your image with the biggest Z-order.
CGImageRef image;
size_t cWitdh = Max_Width;
size_t cHeight = Max_Height;
size_t bitsPerComponent = 8;//If 8 isn't right you should use CGImageGetBitsPerComponent(image) with some image.
size_t bytesPerRow = 4 * Max_Width;// I use 4 assuming you have 8 Bits per component and you have 4 components (R,G,B,A) , if you have an image specifically, you can use CGImageGetBytesPerRow(image)
//Now we build a Context with those dimensions.
CGContextRef context = CGBitmapContextCreate(nil, cWitdh, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));
//The code of this cycle is to ilustrate what you have to do:
for(image in your Images ordered by z-Order)
{
//The location where you draw your image on the context is not always the same location you have in your UIView,
//this could change and you need to calculate that position according to the scale between you images real size, and the size of the UIImage being show on the UIView.
CGContextDrawImage(context, CGRectMake(currentImage.frame.origin.x, currentImage.frame.origin.y, CGImageGetWidth(currentImage), CGImageGetHeight(currentImage), currentImage);
}
CGImageRef mergeResult = CGBitmapContextCreateImage(context);
mergedImage = [[UIImage alloc] initWithCGImage:tmp];
CGContextRelease(context);
CGImageRelease(mergeResult);
return mergedImage;}
希望对你有帮助。