2011-10-21 43 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