2011-10-21 40 views
0

在我的應用程序:改變的UIView bounds屬性在IOS,然後繪製圖像

  • 我有一個視圖(UIView的* MyView的)wihth clipsToBound = YES;

  • 我有一個按鈕做改變邊界財產的來源:

CGRect newRect = myView.bounds; 
newRect.origin.x += 100; 
myView.bounds = newRect; 
myView.layer.frame = newRect; 
  • 然後我從視圖中的形象:
UIGraphicsBeginImageContext(myView.bounds.size); 
[myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil); 

它產生我不期望的圖像。我想要一個圖像,就像我在iPhone屏幕上看到的一樣。

鏈接,這裏的代碼: http://www.mediafire.com/?ufr1q8lbd434wu1

請幫幫我!

+0

我不認爲myView.layer.frame = newRect;是必要的,請嘗試刪除它。 – jbat100

+0

親愛的,移動它不能解決問題。 –

回答

1

你不想在你的上下文中渲染圖像本身 - 它總是以同樣的方式渲染(你並沒有改變圖像,只是移動了它的視圖)。

要呈現這樣的圖像的父視圖:

UIGraphicsBeginImageContext(myView.superview.bounds.size); 
[myView.superview.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil); 

編輯

但是,您可能不希望呈現在您的上海華:)

你的看法一切可能看起來像

MainView 
    ImageView (myView) 
    UIButton (ok button) 
    UIButton (cancel button) 

在這裏,渲染你的圖像的超級視圖將呈現MainView - 包括按鈕!

您需要添加另一個視圖到您的層次結構是這樣的:

MainView 
    UIView (enpty uiview) 
    ImageView (myView) 
    UIButton (ok button) 
    UIButton (cancel button) 

現在,當您呈現圖像的上海華盈,它是隻得到了圖像裏面 - 按鈕沒有得到呈現: )

+0

親愛的deanWombourne。您的解決方案效果很好,它會將圖像渲染爲屏幕中的視圖。但我只想渲染myView,而不是superView。有什麼辦法嗎? –

+0

鏈接我的原始意想不到的代碼在這裏:http://www.mediafire.com/?ufr1q8lbd434wu1 –

+0

你需要添加另一個視圖到你的層次結構 - 看看我的編輯更多細節。 – deanWombourne