Basic ios image rendering based on bitmap

Source: Internet
Author: User

The content includes image watermark, image cropping, screen, and background tile.

1. Image Watermark Function

# Import "UIImage + MJ. h "@ implementation UIImage (MJ) + (instancetype) waterImageWithBg :( NSString *) bg logo :( NSString *) logo {UIImage * bgImage = [UIImage imageNamed: bg]; // 1. create a bitmap-based context (enable a bitmap-based context) uigraphicsbeginimagecontextwitexceptions (bgImage. size, NO, 0.0); // 2. image background [bgImage drawInRect: CGRectMake (0, 0, bgImage. size. width, bgImage. size. height)]; // 3. UIImage * waterImage = [UIImage imageNamed: logo]; CGFloat scale = 0.2; CGFloat margin = 5; CGFloat waterW = waterImage. size. width * scale; CGFloat waterH = waterImage. size. height * scale; CGFloat waterX = bgImage. size. width-waterW-margin; CGFloat waterY = bgImage. size. height-waterH-margin; [waterImage drawInRect: CGRectMake (waterX, waterY, waterW, waterH)]; // 4. obtain the created UIImage object UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext () from the context; // 5. end context UIGraphicsEndImageContext (); return newImage ;}@ end
Call

// 1. return the watermark image UIImage * newImage = [UIImage waterImageWithBg: @ "scene" logo: @ "logo"]; // 2. show image self. iconView. image = newImage;

2. Image Cropping

# Import "UIImage + MJ. h "@ implementation UIImage (MJ) + (instancetype) circleImageWithName :( NSString *) name borderWidth :( CGFloat) borderWidth borderColor :( UIColor *) borderColor {// 1. load the source image UIImage * oldImage = [UIImage imageNamed: name]; // 2. enable the context CGFloat imageW = oldImage. size. width + 2 * borderWidth; CGFloat imageH = oldImage. size. height + 2 * borderWidth; CGSize imageSize = CGSizeMake (imageW, imageH); uigraphicsbeginimagecontextwitexceptions (imageSize, NO, 0.0); // 3. obtain the current context CGContextRef ctx = UIGraphicsGetCurrentContext (); // 4. border (large circle) [borderColor set]; CGFloat bigRadius = imageW * 0.5; // large circle radius CGFloat centerX = bigRadius; // center CGFloat centerY = bigRadius; CGContextAddArc (ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0); CGContextFillPath (ctx); // circle // 5. small Round CGFloat smallRadius = bigRadius-borderWidth; CGContextAddArc (ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0 ); // crop (the things drawn after the cropping will be affected) CGContextClip (ctx); // 6. drawing [oldImage drawInRect: CGRectMake (borderWidth, borderWidth, oldImage. size. width, oldImage. size. height)]; // 7. figure UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // 8. end context UIGraphicsEndImageContext (); return newImage ;}@ End
Call

- (void)viewDidLoad{    [super viewDidLoad];        UIImage *newImage = [UIImage circleImageWithName:@"me" borderWidth:3 borderColor:[UIColor whiteColor]];    self.iconView.image = newImage;        NSData *data = UIImagePNGRepresentation(newImage);    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];    [data writeToFile:path atomically:YES];}

3. Screen

# Import "UIImage + MJ. h "@ implementation UIImage (MJ) + (instancetype) captureWithView :( UIView *) view {// 1. enable context uigraphicsbeginimagecontextwitexceptions (view. frame. size, NO, 0.0); // 2. render the layer of the controller view to the context [view. layer renderInContext: UIGraphicsGetCurrentContext ()]; // 3. retrieve image UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // 4. end context UIGraphicsEndImageContext (); return newImage ;}@ end

Call

-(IBAction) clip {dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1.0 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {/1. capture UIImage * newImage = [UIImage captureWithView: self. view]; // 2. write File NSData * data = UIImagePNGRepresentation (newImage); NSString * path = [using (NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "new.png"]; [data writeToFile: path atomically: YES];});}

4. Stretch the background

- (void)imageBg{    UIImage *oldImage = [UIImage imageNamed:@"me"];        UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);    [oldImage drawInRect:self.view.bounds];    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];}

5. tiled background

-(Void) viewDidLoad {[super viewDidLoad]; // self. view. backgroundColor = [UIColor redColor]; // 1. create a row of background image CGFloat rowW = self. view. frame. size. width; // CGFloat rowH = 40; CGFloat rowH = 30; then (CGSizeMake (rowW, rowH), NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext (); // draw a rectangle box [[UIColor redColor] set]; CGContextAddRect (ctx, CGRectMake (0, 0, rowW, rowH); CGContextFillPath (ctx); // 2. draw line [[UIColor greenColor] set]; CGFloat lineWidth = 2; CGContextSetLineWidth (ctx, lineWidth); CGFloat dividerX = 0; CGFloat dividerY = rowH-lineWidth; struct (ctx, dividerX, dividerY); CGContextAddLineToPoint (ctx, rowW-dividerX, dividerY); CGContextStrokePath (ctx); // 3. figure UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); // 4. end context UIGraphicsEndImageContext (); // 5. set as background self. textView. backgroundColor = [UIColor colorWithPatternImage: newImage];}




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.