更改iOS中UIView的bounds属性,然后渲染图像

发布于 2024-12-11 11:22:18 字数 847 浏览 0 评论 0原文

在我的应用程序中:

  • 我有一个视图 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-12-18 11:22:18

您不想在您的上下文中渲染图像本身 - 它将始终以相同的方式渲染(您没有改变图像,您只是将其视图向上移动多远)。

您想要像这样渲染图像的父视图:

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)

现在,当您渲染图像的超级视图时,它只在其中包含图像 - 按钮不会被渲染:)

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 :

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

EDIT

However, you might not want to render everything in your superview :)

Your views might look something like

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

Here, rendering your image's superview will render MainView - including the buttons!

You need to add another view into your hierachy like this :

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

Now, when you render your image's superview, it's only got the image inside it - the buttons don't get rendered :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文