简体   繁体   中英

Create image on top of another uiimage

I am not able to draw image on top of another image. Below if the code I am using:

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

}

The image which I get from the current context is a black image. I want to draw eraser image on the image which I get from the context.

This is updated code which is working in my side.

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

尝试在rect而不是AtPoint中绘制橡皮擦图像

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

You forgot to draw the bottom image into the context, hence you are drawing your eraser image on top of an uninitialized (black) background. You need to add some code to first draw your desired background image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM