0

在我的应用程序中:

  • 我有一个带有 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

请帮我!

4

1 回答 1

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)

现在,当您渲染图像的超级视图时,它只有图像内部 - 按钮不会被渲染:)

于 2011-10-21T13:46:46.347 回答