[IOS UI advanced, iosui advanced

Source: Internet
Author: User

[IOS UI advanced, iosui advanced
A. Some details about Quiartz2D1. The UIKit tool has encapsulated context references, so you do not need to manually obtain and render them.

1 - (void)drawRect:(CGRect)rect {2     [[UIColor redColor] set];3     UIRectFill(CGRectMake(0, 0, 100, 100));4 }
1-(void) drawRect :( CGRect) rect {2 CGContextRef ctx = UIGraphicsGetCurrentContext (); 3 4 // 1.1 create the first path 5 CGMutablePathRef linePath = CGPathCreateMutable (); 6 7 // 1. 2. depict path 8 CGPathMoveToPoint (linePath, NULL, 0, 0); 9 CGPathAddLineToPoint (linePath, NULL, 100,100); 10 11 // 1. 3. add linePath to context 12 CGContextAddPath (ctx, linePath); 13 14 // 2 Add a circle path15 CGMutablePathRef circlePath = CGPathCreateMutable (); 16 rows (circlePath, NULL, CGRectMake (0, 0, 50, 50); 17 CGContextAddPath (ctx, circlePath); 18 19 // rendering context 20 CGContextStrokePath (ctx); 21 22 // release path23 CGPathRelease (linePath ); 24 CGPathRelease (circlePath); 25}1 // 2 // MyImageView. h 3 // MyImageView 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import <UIKit/UIKit. h> 10 11 @ interface MyImageView: UIView12 13 @ property (nonatomic, weak) UIImage * image; 14 15 @ end

 

1 // 2 // MyImageView. m 3 // MyImageView 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "MyImageView. h "10 11 @ implementation MyImageView12 13-(void) setImage :( UIImage *) image {14 _ image = image; 15 16 // when the image is reset, redraw 17 [self setNeedsDisplay]; 18} 19 20-(void) drawRect :( CGRect) rect {21 [self. image drawInRect: rect]; 22} 23 24 @ end25 26 controller: 27-(void) viewDidLoad {28 [super viewDidLoad]; 29 // Do any additional setup after loading the view, typically from a nib.30 31 MyImageView * imageView = [[MyImageView alloc] init]; 32 imageView. frame = CGRectMake (0, 0,200,320); 33 [self. view addSubview: imageView]; 34 imageView. image = [UIImage imageNamed: @ "M4"]; 35}
1 // 2 // MyTextView. h 3 // MyTextField 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import <UIKit/UIKit. h> 10 11 @ interface MyTextView: UITextView12 13 @ property (nonatomic, copy) NSString * placeHolderText; 14 15 @ end 
1 // 2 // MyTextView. m 3 // MyTextField 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "MyTextView. h "10 11 @ interface MyTextView () <UITextViewDelegate> 12 13/** original text color */14 @ property (nonatomic, weak) UIColor * originalTextColor; 15 16 @ end17 18 @ implementation MyTextView19 20 // rewrite the initialization method and set proxy 21-(instancetype) init {22 if (self = [super init]) {23 self. returnKeyType = UIReturnKeyDone; 24 self. delegate = self; 25} 26 27 return self; 28} 29 30 // when you reset placeHolderText, repaint the control 31-(void) setPlaceHolderText :( NSString *) placeHolderText {32 _ placeHolderText = placeHolderText; 33 [self setNeedsDisplay]; 34} 35 36 // start editing, remove placeHolder37-(BOOL) textViewShouldBeginEditing :( UITextView *) textView {38 if ([self. text isw.tostring: self. placeHolderText]) {39 self. text = nil; 40 self. textColor = self. originalTextColor; 41} 42 43 return YES; 44} 45 46 // end editing. If the text is empty, set placeHolder47-(void) textViewDidEndEditing :( UITextView *) textView {48 [self setNeedsDisplay]; 49} 50 51-(void) drawRect :( CGRect) rect {52 if (self. text. length = 0) {53 self. text = self. placeHolderText; 54 self. textColor = [UIColor grayColor]; 55} 56} 57 58 59 @ end
D. Image Watermark1. STEP (1) drag a UIImageView on the storyboard and create a bitmap-based context in the image context created by the Controller code --> the system creates a bitmap object, which is equivalent to creating a new UIImage object (2) painting background (3) watermarking (4) Obtaining the created UIImage object from the context (5) Ending image Context
1 // 2 // UIImage + HW. m 3 // Watermark 4 // 5 // Created by hellovoidworld on 14/12/31. 6 // Copyright (c) 2014 hellovoidworld. all rights reserved. 7 // 8 9 # import "UIImage + HW. h "10 11 @ implementation UIImage (HW) 12 13 + (instancetype) image :( NSString *) image withWatermark :( NSString *) watermark {14 // obtain the background image 15 UIImage * bgImage = [UIImage imageNamed: image]; 16 17 // 1. enable a bitmap Context 18 uigraphicsbeginimagecontextwitexceptions (bgImage. size, NO, 0.0); 19 20 // 2. add the background image to the position graph context 21 [bgImage drawInRect: CGRectMake (0, 0, bgImage. size. width, bgImage. size. height)]; 22 23 // 3. add watermark image 24 UIImage * watermarkImage = [UIImage imageNamed: watermark]; 25 CGFloat scale = 0.5; 26 CGFloat margin = 50; 27 CGFloat watermarkWidth = watermarkImage. size. width * scale; 28 CGFloat watermarkHeight = watermarkImage. size. width * scale; 29 CGFloat watermarkX = bgImage. size. width-watermarkWidth-margin; 30 CGFloat watermarkY = bgImage. size. height-watermarkHeight-margin; 31 [watermarkImage drawInRect: CGRectMake (watermarkX, watermarkY, watermarkWidth, watermarkHeight)]; 32 33 34 // 4. obtain the synthesized image 35 UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext (); 36 37 // 5. disable text context 38 UIGraphicsEndImageContext (); 39 40 return resultImage; 41} 42 43 @ end
(6) display to UIImageView
 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     // Do any additional setup after loading the view, typically from a nib. 4     5     UIImage *watermarkedImage = [UIImage image:@"M4" withWatermark:@"a9ec8a13632762d0092abc3ca2ec08fa513dc619"]; 6     7     UIImageView *imageView = [[UIImageView alloc] initWithImage:watermarkedImage]; 8     imageView.frame = CGRectMake(0, 0, 200, 320); 9     [self.view addSubview:imageView];10 }
(7) Save image a. Compress image B. Write File
1 // The compressed image is a PNG binary data 2 NSData * data = UIImagePNGRepresentation (watermarkedImage); 3 4 // Write File 5 NSString * path = [inline (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "watermarkedImage.png"]; 6 NSLog (@ "% @", path); 7 [data writeToFile: path atomically: YES];
1/** create a circular Avatar image with a specified width and color border */2 + (instancetype) imageOfCircleHeadIcon :( NSString *) image withBorderWidth :( CGFloat) borderWidth borderColor :( UIColor *) borderColor {3 // retrieve image 4 UIImage * headIconImage = [UIImage imageNamed: image]; 5 6 // enable image context 7 CGFloat backImageWidth = headIconImage. size. width + 2 * borderWidth; 8 CGFloat backImageHeight = headIconImage. size. height + 2 * borderWidth; 9 CGSize backImageSize = CGSizeMake (backImageWidth, backImageHeight); 10 bytes (backImageSize, NO, 0.0); 11 12 // depicts the background circle 13 [borderColor set]; // set the ring color 14 CGContextRef ctx = UIGraphicsGetCurrentContext (); 15 CGFloat backCircleRadius = backImageWidth * 0.5; // large circle radius 16 CGFloat backCircleX = backCircleRadius; // X17 CGFloat backCircleY = backCircleRadius; // Y18 percentile (ctx, backCircleX, backCircleY, round, 0, M_PI * 2, 0); 19 CGContextFillPath (ctx ); 20 21 // depict the circle 22 CGFloat border = backCircleRadius-borderWidth used to display the image; // The circle radius 23 CGFloat frontCircleX = backCircleX; 24 CGFloat frontCircleY = backCircleY; 25 (ctx, frontCircleX, frontCircleY, frontCircleRadius, 0, M_PI * 2, 0); 26 27 // crop (the image depicted later will be cropped) 28 CGContextClip (ctx ); 29 30 // Add the image to context 31 [headIconImage drawInRect: CGRectMake (borderWidth, borderWidth, headIconImage. size. width, headIconImage. size. height)]; 32 33 // get the synthesized image 34 UIImage * headIconResultImage = merged (); 35 36 // close the image context 37 UIGraphicsEndImageContext (); 38 39 return headIconResultImage; 40}1/** click "screen" */2-(IBAction) screenShotcut {3 // delay to prevent the status of the button being pressed when it is intercepted. 4 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (0.1 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {5 6 UIView * view = self. view; 7 8 // 1. enable bitmap Context 9 uigraphicsbeginimagecontextwitexceptions (view. frame. size, NO, 0.0); 10 11 // 2. rendering controller view layer to context 12 [view. layer renderInContext: UIGraphicsGetCurrentContext ()]; 13 14 // 3. obtain image 15 UIImage * screenImage = UIGraphicsGetImageFromCurrentImageContext (); 16 17 // 4. end Context 18 UIGraphicsEndImageContext (); 19 20 // Store Image 21 NSData * data = UIImagePNGRepresentation (screenImage); 22 NSString * path = [[terminate (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "screenShot.png"]; 23 NSLog (@ "% @", path); 24 [data writeToFile: path atomically: YES] ;}); 25 26}1-(void) viewDidLoad {2 [super viewDidLoad]; 3 // Do any additional setup after loading the view, typically from a nib. 4 5 // enable image context 6 CGFloat rowW = self. view. frame. size. width; 7 CGFloat rowH = 30; 8 uigraphicsbeginimagecontextwittions (CGSizeMake (rowW, rowH), NO, 0.0); 9 10 CGContextRef ctx = UIGraphicsGetCurrentContext (); 11 // Color Block Background 12 [[UIColor redColor] set]; 13 CGContextAddRect (ctx, CGRectMake (0, 0, rowW, rowH); 14 CGContextFillPath (ctx ); 15 16 // draw line 17 [[UIColor greenColor] set]; 18 CGFloat lineWidth = 2; 19 CGContextSetLineWidth (ctx, lineWidth); 20 CGFloat lineX = 0; 21 CGFloat lineY = rowH-lineWidth; 22 CGContextMoveToPoint (ctx, lineX, lineY); 23 CGContextAddLineToPoint (ctx, rowW-lineX, lineY); 24 CGContextStrokePath (ctx ); 25 26 UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); 27 28 // fill the screen in Tiled mode 29 [self. view setBackgroundColor: [UIColor colorWithPatternImage: image]; 30 31 UIGraphicsEndImageContext (); 32}

 

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.