【问题标题】:Convert content of UIScrollView in UIImage在 UIImage 中转换 UIScrollView 的内容
【发布时间】:2012-05-12 16:29:20
【问题描述】:

在我的应用程序中,用户可以将UIImageView 添加为我的UIScrollView 的子视图。当用户点击“保存”按钮时,我想将我的UIScrollView 的所有内容作为UIImage 保存到照片库中。

我一直在谷歌上寻找,但没有太多关于这个主题的内容。

编辑:下面的代码完成了这项工作

        CGRect rect = self.mainView.frame;
        [self.mainView setFrame:CGRectMake(0, 0, 824, self.mainView.contentSize.height)];
        UIGraphicsBeginImageContext(self.mainView.bounds.size);
        [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(image , nil, nil, nil);
        [self.mainView setFrame:rect];

【问题讨论】:

    标签: objective-c uiscrollview uiimage photolibrary


    【解决方案1】:

    看来您需要截屏,您可以在完成操作时尝试此代码 -

    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:@"myImage.png" atomically:YES];
    

    【讨论】:

    • 不要忘记在渲染前调整滚动视图的大小以适应内容,然后将其调整回原来的大小。
    • 第二行出现此错误:Receiver type 'CALayer' for instance message is a forward declaration
    • 添加这个 - #import
    • 非常感谢 rishi 和 Leo Natan
    猜你喜欢
    • 2010-10-25
    • 2020-01-30
    • 2012-01-27
    • 1970-01-01
    • 2013-09-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多