更改iOS中UIView的bounds属性,然后渲染图像
在我的应用程序中:
我有一个视图 (UIView *myView) ,其中 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
请帮助我!
In my app:
I have a view (UIView *myView) wihth clipsToBound = YES;
I have a button to do changing the origin of bounds property:
CGRect newRect = myView.bounds; newRect.origin.x += 100; myView.bounds = newRect; myView.layer.frame = newRect;
- Then I get the image from the view:
UIGraphicsBeginImageContext(myView.bounds.size); [myView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil);
It produces the image I don't expected. I want a image as I see in the iPhone's screen.
Link for the code here:
http://www.mediafire.com/?ufr1q8lbd434wu1
Please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想在您的上下文中渲染图像本身 - 它将始终以相同的方式渲染(您没有改变图像,您只是将其视图向上移动多远)。
您想要像这样渲染图像的父视图:
编辑
但是,您可能不想渲染超级视图中的所有内容:)
您的视图可能看起来像
这样,渲染图像的超级视图将渲染 MainView - 包括按钮!
您需要将另一个视图添加到您的层次结构中,如下所示:
现在,当您渲染图像的超级视图时,它只在其中包含图像 - 按钮不会被渲染:)
You don't want to render the image itself in your context - that will always render in the same way (you're not altering the image, you're jsut moving how far up the view it is).
You want to render the image's parent view like this :
EDIT
However, you might not want to render everything in your superview :)
Your views might look something like
Here, rendering your image's superview will render MainView - including the buttons!
You need to add another view into your hierachy like this :
Now, when you render your image's superview, it's only got the image inside it - the buttons don't get rendered :)