【问题标题】:convert uiview to .png image将 uiview 转换为 .png 图像
【发布时间】:2015-06-24 15:42:32
【问题描述】:

我在 iphone 上工作,我采用 UIView 的子类,并在 draw rect 方法中进行一些设计。我想将此视图转换为 .png 格式。

提前致谢。

【问题讨论】:

  • 你想得到那个uiView的截图吗??
  • 您希望将图像转换为 .png 格式或截屏

标签: iphone objective-c xcode


【解决方案1】:
    UIGraphicsBeginImageContext(myView.frame.size);
    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data=UIImagePNGRepresentation(viewImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"myimage.png"];
    [data writeToFile:strPath atomically:YES];

【讨论】:

  • 感谢它在控制台中显示图像但是当我在 imageview 上显示它时它没有显示帮助我
  • 检查您的 IBOutlet 是否设置了 imageview
【解决方案2】:
   UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(keyWindow.bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
    UIGraphicsEndImageContext();

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 2020-06-15
    • 2012-06-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多