【问题标题】:Save UIView's representation to file将 UIView 的表示保存到文件
【发布时间】:2011-03-04 08:35:21
【问题描述】:

将 UIView 的表示保存到文件的最简单方法是什么?

我的解决方案是,

 UIGraphicsBeginImageContext(someView.frame.size);
 [someView drawRect:someView.frame];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 NSString* pathToCreate = @"sample.png";

 NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
 [imageData writeToFile:pathToCreate atomically:YES];

但这似乎很棘手,我认为必须有更有效的方法来做到这一点。

【问题讨论】:

    标签: iphone image uiview save


    【解决方案1】:

    你也可以像这样使用图层:

    UIGraphicsBeginImageContext(someView.bounds.size);
    [someView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    【讨论】:

    • 我明白了。好像比我的好。 :)
    • 如果你想在 iPhone 4 上实现双分辨率,请使用 UIGraphicsBeginImageContextWithOptions
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多