IOS learning and ios development

Source: Internet
Author: User

IOS learning and ios development
Draw an in-place chart

The following uses the bitmap image context to add a watermark to an image. In the following program, we first create the context and then draw the image, line, and text in the context, finally, the new image is displayed to the interface from the context of the current bitmap.

-(Void) viewDidLoad {[super viewDidLoad]; UIImage * image = [self drawImageAtImageContext]; UIImageView * imageView = [[UIImageView alloc] initWithImage: image]; imageView. center = CGPointMake (160,282); [self. view addSubview: imageView];} // Add a watermark-(UIImage *) drawImageAtImageContext {// obtain a bitmap image context CGSize size = CGSizeMake (300,188 ); // canvas size UIGraphicsBeginImageContext (size); UIImage * image = [UIImage imageNamed: @ "frame_shop_lovely_bg@2x.jpg"]; // note that the position of the drawing is relative to the canvas vertex, not the screen [image drawInRect: CGRectMake (0, 0,300,188)]; // Add the watermark CGContextRef ref = UIGraphicsGetCurrentContext (); // The character length and font UIFont * font = [UIFont systemFontOfSize: 15]; NSString * str = @ "Jin Mao"; int strLength = str. length * font. pointSize; // determine a straight line at two points // The length of the underline is equal to the length of CGContextMoveToPoint (ref, 200,178); CGContextAddLineToPoint (ref, strLength + 200,178 ); // The color and width of the straight line [[UIColor redColor] setStroke]; CGContextSetLineWidth (ref, 2); // draw the image to the specified image context, only the border CGContextDrawPath (ref, kCGPathStroke ); // character location [str drawInRect: CGRectMake (200,158,100, 30) withAttributes :@ {NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor redColor]}]; // return the newly drawn image UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // do not forget to close the context UIGraphicsEndImageContext (); return newImage ;}

From KenshinCui

Related Article

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.