在另一个uiimage上创建图像

时间:2014-04-14 10:23:19

标签: ios objective-c iphone ios7 cgcontext

我无法在另一幅图像上绘制图像。如果我正在使用的代码如下:

-(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();

}

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

3 个答案:

答案 0 :(得分:3)

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

-(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_;
}

答案 1 :(得分:0)

尝试在反应中绘制您的橡皮擦图像,而不是At Point

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

答案 2 :(得分:0)

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