【问题标题】:Saving a UIImage from a UIView not working properly从 UIView 保存 UIImage 无法正常工作
【发布时间】:2013-01-02 05:23:30
【问题描述】:

我正在尝试将 UIView(及其子视图)的内容保存到 UIImage 中。

这是我正在使用的代码:

+(UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions([view bounds].size, YES, 0);
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

效果很好……第一次。但是,如果我随后修改视图的子视图并再次调用此方法,我每次都会得到旧的 UIImage。

据我了解,UIGraphicsEndImageContext() 实际上确实将位图图像从堆栈中弹出。

如何让这个 imageWithView: 方法工作,同时考虑到视图子视图的当前状态?

【问题讨论】:

  • 老兄,我无法重现您提到的问题。我用你的代码试过了,在改变视图的子视图后它工作得很好。请参阅此屏幕截图cl.ly/image/0W1A1k3p121Q。请确保您没有在旧图像的变量中保存先前的引用。

标签: objective-c ios uiimage core-graphics


【解决方案1】:

我希望这会有所帮助:

UIGraphicsBeginImageContext(self.view.frame.size);
[testView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多