【问题标题】:changing the bounds property of UIView in iOS, then rendering image在iOS中更改UIView的bounds属性,然后渲染图像
【发布时间】:2011-10-21 13:32:47
【问题描述】:

在我的应用中:

  • 我有一个带有 clipsToBound = YES 的视图 (UIView *myView);

  • 我有一个按钮可以更改边界属性的原点:

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

请帮帮我!

【问题讨论】:

  • 我不认为 myView.layer.frame = newRect;是必要的,请尝试删除它。
  • 亲爱的,移动它并不能解决问题。

标签: iphone ios render layer bounds


【解决方案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)

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

【讨论】:

  • 尊敬的沃姆伯恩院长。您的解决方案效果很好,它将图像呈现为屏幕中的视图显示。但我只想渲染 myView,而不是 superView。有什么办法吗?
  • 链接我的原始意外代码:mediafire.com/?ufr1q8lbd434wu1
  • 您需要在层次结构中添加另一个视图 - 请查看我的编辑以了解更多详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多