组合多个UIImageView并保留分辨率

时间:2010-05-08 18:20:41

标签: iphone uiimageview uiimage

我正在设计一个应用程序,用户将多个UIImageViews放在另一个上面。当用户决定将其保存到相册时,我必须将所有这些UIImageViews组合并将其保存到Photo Library。在组合它们时,我需要保留它们的位置,分辨率,zorder。我试过的方法是让一个父UIView就像一个容器。用户将所有UIImageViews放在此UIView中。保存时,我会截取UIView的截图并保存。虽然这种方法有效,但它不能保持分辨率。最终图像的分辨率与父UIView大小的分辨率相同(宽度和高度为300像素)。有没有办法保留决议?或者至少有更高的分辨率,如高达1024 x 768像素?任何指针/代码示例都将不胜感激!

1 个答案:

答案 0 :(得分:2)

如果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;}

希望它有所帮助。