2010-05-08 40 views
2

我正在設計一個應用程序,其中一個用戶將多個UIImageView放在另一個之上。當用戶決定將其保存到相冊時,我必須將所有這些UIImageView合併並保存到照片庫中。結合他們時,我需要保留他們的立場,決議,zorder。我試過的方法是讓父UIView像容器一樣工作。用戶將所有的UIImageView放在這個UIView中。保存時,我截取UIView並保存。雖然這種方法有效,但它不能保留分辨率。最終圖像的分辨率與父UIView大小(寬度和高度爲300像素)的分辨率相同。有辦法保存這個決議嗎?或至少有像1024 x 768像素更高的分辨率?任何指針/代碼示例將不勝感激!結合多個UIImageViews並保留分辨率

回答

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

希望它能幫助。