【问题标题】:Create image on top of another uiimage在另一个 uiimage 之上创建图像
【发布时间】:2014-04-14 10:23:18
【问题描述】:

我无法在另一个图像上绘制图像。下面是我使用的代码:

-(void)createImage
{

// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(imgVw_noHair_.frame.size, YES, 0.0);

// get context
CGContextRef context = UIGraphicsGetCurrentContext();

// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);

// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
[eraser drawAtPoint:CGPointMake(50, 50)];

// pop context
UIGraphicsPopContext();

UIImage _image_ = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

我从当前上下文中获得的图像是黑色图像。我想在从上下文中获得的图像上绘制橡皮擦图像。

【问题讨论】:

  • 你为什么要画它,你可以有imageview的权利..!!
  • 你看看这个答案..stackoverflow.com/a/1480838/1865424
  • @Stark - 当我使用相同的代码而不是圆圈绘制图像时,会出现警告并且没有显示任何内容。 CGContextRestoreGState: invalid context 0x0

标签: ios objective-c iphone ios7 cgcontext


【解决方案1】:

这是在我身边工作的更新代码。

-(void)createImage
{

   //Create context in which you have to draw
    UIGraphicsBeginImageContextWithOptions(imgvwImage.image.size, YES, 0.0);

    // get context
    CGContextRef context = UIGraphicsGetCurrentContext();

    // push context to make it current
    // (need to do this manually because we are not drawing in a UIView)
    UIGraphicsPushContext(context);

    //draw the old image in that context
    [imgvwImage.image drawInRect:CGRectMake(0, 0, 200, 200)];

    UIImage *img=[UIImage imageNamed:@"img.png"];

    //Draw your image in that context
    [img drawAtPoint:CGPointMake(50, 50)];

    // pop context
    UIGraphicsPopContext();



     UIImage *image_ = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    //Get that image
    imgvwImage.image=image_;
}

【讨论】:

    【解决方案2】:

    尝试用矩形而不是 AtPoint 绘制橡皮擦图像

    [eraser drawInRect:CGRectMake(50, 50, width, height)];
    

    【讨论】:

      【解决方案3】:

      您忘记将底部图像绘制到上下文中,因此您将eraser 图像绘制在未初始化(黑色)背景之上。您需要添加一些代码来首先绘制您想要的背景图像。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多