ios 基本圖形的繪制 基於bitmap 位圖 – iPhone手機開發技術文章 iPhone軟體開發教學課程

內容包括 圖片水印,圖片裁剪,屏幕截圖,背景平鋪

1、圖片水印功能

#import "UIImage+MJ.h"

@implementation UIImage (MJ)
+ (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo
{
    UIImage *bgImage = [UIImage imageNamed:bg];
    
    // 1.創建一個基於位圖的上下文(開啟一個基於位圖的上下文)
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    
    // 2.畫背景
    [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.從上下文中取得制作完畢的UIImage對象
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 5.結束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end

調用

 
    // 1.返回水印圖片
    UIImage *newImage = [UIImage waterImageWithBg:@"scene" logo:@"logo"];
    
    // 2.顯示圖片
    self.iconView.image = newImage;

2、圖片裁剪

#import "UIImage+MJ.h"

@implementation UIImage (MJ)
+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    // 1.加載原圖
    UIImage *oldImage = [UIImage imageNamed:name];
    
    // 2.開啟上下文
    CGFloat imageW = oldImage.size.width + 2 * borderWidth;
    CGFloat imageH = oldImage.size.height + 2 * borderWidth;
    CGSize imageSize = CGSizeMake(imageW, imageH);
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    
    // 3.取得當前的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 4.畫邊框(大圓)
    [borderColor set];
    CGFloat bigRadius = imageW * 0.5; // 大圓半徑
    CGFloat centerX = bigRadius; // 圓心
    CGFloat centerY = bigRadius;
    CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
    CGContextFillPath(ctx); // 畫圓
    
    // 5.小圓
    CGFloat smallRadius = bigRadius - borderWidth;
    CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
    // 裁剪(後面畫的東西才會受裁剪的影響)
    CGContextClip(ctx);
    
    // 6.畫圖
    [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];
    
    // 7.取圖
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 8.結束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end

調用

- (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、屏幕截圖

#import "UIImage+MJ.h"

@implementation UIImage (MJ)
+ (instancetype)captureWithView:(UIView *)view
{
    // 1.開啟上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    // 2.將控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.結束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end

調用

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

4,背景拉伸

- (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、背景平鋪

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    self.view.backgroundColor = [UIColor redColor];
    
    // 1.創建一行背景圖片
    CGFloat rowW = self.view.frame.size.width;
//    CGFloat rowH = 40;
    CGFloat rowH = 30;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 畫矩形框
    [[UIColor redColor] set];
    CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
    CGContextFillPath(ctx);
    
    // 2.畫線
    [[UIColor greenColor] set];
    CGFloat lineWidth = 2;
    CGContextSetLineWidth(ctx, lineWidth);
    CGFloat piderX = 0;
    CGFloat piderY = rowH - lineWidth;
    CGContextMoveToPoint(ctx, piderX, piderY);
    CGContextAddLineToPoint(ctx, rowW - piderX, piderY);
    CGContextStrokePath(ctx);
    
    // 3.取圖
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.結束上下文
    UIGraphicsEndImageContext();
    
    // 5.設置為背景
    self.textView.backgroundColor = [UIColor colorWithPatternImage:newImage];
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *