【问题标题】:Get UIImage from view's layers?从视图的图层中获取 UIImage?
【发布时间】:2011-06-27 12:43:52
【问题描述】:

我有一个视图,其中包含内容为图像的子层。

我希望通过 myView.image 获取视图的图像,但显然视图没有图像,只有图层。

如何从视图层创建 UIImage?

【问题讨论】:

    标签: iphone uiimage calayer


    【解决方案1】:
    UIGraphicsBeginImageContext(yourView.bounds.size);
    [yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    注意:您需要为此包含 QuartzCore。

    【讨论】:

      【解决方案2】:
      CGRect myRect =CGRectMake(0, 0, 300, 300);// [self.view bounds];
      UIGraphicsBeginImageContext(myRect.size);
      
      CGContextRef ctx = UIGraphicsGetCurrentContext();
      [[UIColor blackColor] set];
      CGContextFillRect(ctx, myRect);
      
      [self.view.layer renderInContext:ctx];
      UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        相关资源
        最近更新 更多