繁体   English   中英

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

[英]changing the bounds property of UIView in iOS, then rendering image

在我的应用中:

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

  • 我有一个按钮可以更改bounds属性的来源:

 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

请帮我!

您不想在上下文中渲染图像本身-总是以相同的方式渲染(您没有更改图像,只是在移动视图的远处)。

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

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM