A brief analysis on IOS's method of watermark to pictures _ios

Source: Internet
Author: User

Not much to say, directly on the code

A new class of UIImage, declared in. h

+ (UIImage *) Imagewithimage: (uiimage *) image content: (NSString *) content frame: (cgrect) frame;

. m below

+ (UIImage *) Imagewithimage: (uiimage *) image content: (NSString *) content frame: (CGRect) Frame {
 //Open Graphics ' context '
 Uigraphicsbeginimagecontextwithoptions (image.size, NO, 0);
 Draw native Pictures
 [image Drawatpoint:cgpointzero];
 Draw the text
 nsstring *str = content on the native graph;
 Create a dictionary of text properties
 nsdictionary *dictionary = @{nsforegroundcolorattributename: [Uicolor Blackcolor], Nsfontattributename: [Uifont systemfontofsize:20]};
 Draw text Attributes
 [str drawinrect:frame withattributes:dictionary];
 Gets the modified picture from the current context
 uiimage *imagenew = Uigraphicsgetimagefromcurrentimagecontext ();
 End Graphics Context
 uigraphicsendimagecontext ();
 return imagenew;
}

but it is to be drawInRect: withAttributes: noted that The method can be used after iOS7.0, when using this method , it is necessary to see whether the system is appropriate or to add judgment to the method.

 Double device = [[Uidevice currentdevice].systemversion doublevalue];
 if (device >= 7.0f) {
  //do something
 }

Then import the header file in VC, create a ImageView

 Call method to pass in an Image object, the text and text you want to add
 uiimage *image = [UIImage imagewithimage:[uiimage imagenamed:@ "3"] content:@ " Illidan "Frame: CGRectMake (M, M, M      )];
 Uiimageview *imageview = [[Uiimageview alloc]initwithframe:cgrectmake (0, 0, Kscreenwidth, kscreenheight)];
 Imageview.image = image;
 [Self.view Addsubview:imageview];

The effect of the following figure, the simple implementation of the image watermark on the function, if you want to change the size of text and color can be modified in the method. Or add a dictionary parameter to the method for a text attribute.

The above is in the iOS to the picture watermark all content, this article has given the example code, believed to everybody understanding to be helpful, hoped that can develop the iOS to be helpful to everybody.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.