iOS 绘图技术

2017-03-28  本文已影响57人  张三儿

近日项目做开发时,利用了绘图技术。做一下绘图总结

// 3.获取图片
    UIImage* image = [UIImage imageNamed:@"me"];

    // 1.开启图片类型的图形上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
   
    // 5.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 4.画一个裁剪的图片
    CGContextAddArc(ctx, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5, 0, 2 * M_PI, 1);

    // 6.裁剪
    CGContextClip(ctx);

    // 7.把图片画上去
    [image drawAtPoint:CGPointZero];

    // 8.取出来
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // 2.关闭图片类型的图形上下文
    UIGraphicsEndImageContext();

    // 测试
    self.imageView.image = newImage;
// 3.获取图片
UIImage* image = [UIImage imageNamed:@"scene"];
// 1.开启图片类型的图形上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 6.画大图
 [image drawAtPoint:CGPointZero];
// 4.文字
NSString* str = @"黑马13期";
// 5.画文字水印
[str drawAtPoint:CGPointMake(20, 20) withAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:20] }];
 // 7.图片
UIImage* logo = [UIImage imageNamed:@"logo"];
 // 8.画图片水印
 [logo drawAtPoint:CGPointMake(image.size.width * 0.6, image.size.height * 0.7)];
 // 取图片
image = UIGraphicsGetImageFromCurrentImageContext();
[image drawAtPoint:CGPointZero];
// 保存到相册
UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
// 2.关闭图片类型的图形上下文
 UIGraphicsEndImageContext();

我项目的案例类似于图片上添加图片

UIImage * img1 = [self imageWithName:@"But_hilight" size:CGSizeMake(100, 25) withCarName:model.carplate];
            UIImage * img2 = [self imageWithName:@"icon_rail"  size:CGSizeMake(30, 30)];
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 60), NO, 0);
            
            [img1 drawInRect:CGRectMake(0, 5, 100, 25)];
            [img2 drawInRect:CGRectMake(40, 30, 30, 30)];
            UIImage *Annotationimage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
- (UIImage *)imageWithName:(NSString *)imgName size:(CGSize)size withCarName:(NSString*) carName {
    
    if (!imgName || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    UIImage *img = [UIImage imageNamed:imgName];
    [img drawAtPoint:CGPointZero];
    NSString *str = carName;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor]; // 文字颜色
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:18]; // 字体
    [str drawInRect:CGRectMake(10, 1, size.width, size.height) withAttributes:dict];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (UIImage *)imageWithName:(NSString *)imgName size:(CGSize)size {
    
    if (!imgName || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    UIImage *img = [UIImage imageNamed:imgName];
    [img drawAtPoint:CGPointZero];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}
上一篇 下一篇

猜你喜欢

热点阅读