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