2012-02-16 61 views
0

我有一個imageview,這個imageview內的圖像是給你從photoroll中選擇。我也有一個按鈕,當你點擊這個按鈕時,有一個圖像通過addSubview代碼添加到視圖中。這張圖片可以拖動,調整大小和旋轉。繪製圖像約束方面

一個問題,當我完成圖像時,我使用drawInRect方法。這將所有圖層繪製到彼此上並創建圖像。但是圖層位置錯誤,尺寸錯誤。它也永遠不會旋轉。我不知道如何解決這個問題,這段代碼就在這段文字的下面。是否有可能保持原始圖像的大小,並仍然在同一個地方繪製圖層,我將它們拖到imageview上,如果不是,我如何爲此創建一個新的大小並獲得我想要的結果。如何繪製旋轉的圖像。

UIGraphicsBeginImageContext(imageView2.image.size);  
// Draw image1 
[imageView2.image drawInRect:CGRectMake(0, 0, imageView2.image.size.width, imageView2.image.size.height)];  
// Draw image2 
for(UIImageView *viewsSub in [self.imageViewer subviews]) 
{    
    [viewsSub.image drawInRect:CGRectMake(viewsSub.frame.origin.x, viewsSub.frame.origin.y, viewsSub.frame.size.width, viewsSub.frame.size.height)]; 
}  
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
pld.imageChosen2 = resultingImage;  
UIGraphicsEndImageContext(); 

回答

0

所以,你想要像你的實際imageview(包括子視圖)「截圖」的東西,是嗎?

我用這段代碼做類似的事情,但不知道是否會爲你工作。

 - (UIImage *)screenshot { 
     CGFloat scale = [UIScreen screenScale]; 

     if(scale > 1.5) { 
      UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale); 
     } else { 
      UIGraphicsBeginImageContext(self.frame.size); 
     } 
     [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return screenshot; 
} 

您應該將ImageView的(包含所有要添加的子視圖之一)內添加此方法。

+0

很有效,非常感謝! – 2012-02-16 15:56:52