Tuesday, April 23, 2013

Creating Watermarked Images from iOS app

Hello Guyz, In this section i will give you a bunch of code for creating a watermarked images. For example: Images with copyright text on top of it.  It sounds difficult but believe me, I have a really simple solution for you. In this part,  I am only merging  two of the images (one is watermark image and another is the image in which you want to put watermark) and hence making them one. So before we proceed, remember you should have watermark image and main image(picture) in your project resources.

-(UIImage *) generateWatermarkForImage:(UIImage *) mainImg{
      UIImage *backgroundImage = mainImg;
    UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"]; 



 //Now re-drawing your  Image using drawInRect method
    UIGraphicsBeginImageContext(backgroundImage.size);
    [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];

 // set watermark position/frame a s(xposition,yposition,width,height)
   // [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];

     [watermarkImage drawInRect:CGRectMake(100, 300, 400, 50)];

// now merging two images into one
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

Now, invoke above method whenever you want to create watermarked images from your main plain image.
    UIImage *backgroundImage = [UIImage imageNamed:@"yourMainImage.png"];
    UIImage *waterMarkedImg = [self generateWatermarkForImage:backgroundImage];

  //use waterMarkedImg as you want to !!

Happy Coding !!!!

2 comments: