iOS开发 ,将多张图片叠加绘制到一起

2017-12-08  本文已影响98人  MCWorld

这种绘制是根据图片的像素比例 等比例进行绘制的,在选择图片和创建展示图片的imageView 时,注意查看尺寸 注:绘图时使用  [UIScreen mainScreen].scale 可以是图片更清晰 UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//这样就不模糊了

//图片上添加文字 详细版

- (UIImage*)text:(NSString*)text addToView:(UIImage*)image{

//设置字体样式

UIFont*font = [UIFont fontWithName:@"Arial-BoldItalicMT"size:100];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];

[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 1)];

[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:100] range:NSMakeRange(0, text.length)];

//    CGSize textSize = [text sizeWithAttributes:dict];

CGSize textSize = [str size];

//绘制上下文

UIGraphicsBeginImageContext(image.size);

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//这样就不模糊了

[image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];

//    int border =10;

CGRect re = {CGPointMake((image.size.width- textSize.width)/2, 200), textSize};

//    CGRect rect = CGRectMake(0, 0, image.size.width, 500);

//此方法必须写在上下文才生效

[str drawInRect:re ];

UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

//修改图片尺寸

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext(newSize);//这样压缩的图片展示出来会很模糊

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//这样就不模糊了

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//这样就不模糊了

// Tell the old image to draw in this new context, with the desired

// new size

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Get the new image from the context

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

// End the context

UIGraphicsEndImageContext();

// Return the new image.

return newImage;

}

//圆角

- (UIImage *) getRadioImaeg:(NSString *)imageName1{

UIImage *image1 = [UIImage imageNamed:imageName1];

UIGraphicsBeginImageContextWithOptions(image1.size, 0, 0);

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(00, 0, image1.size.width, image1.size.width);

CGContextAddEllipseInRect(ctx, rect);

CGContextClip(ctx);

[image1 drawInRect:rect];

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;

}

//图片叠加

- (UIImage *)addImage:(NSString *)imageName1 withImage:(NSString *)imageName2 {

UIImage *image1 = [UIImage imageNamed:imageName1];

UIImage *image2 = [self getRadioImaeg:@"333"];

UIGraphicsBeginImageContext(image1.size);

//UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);//这样就不模糊了

[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

[image2 drawInRect:CGRectMake((image1.size.width - image2.size.width)/2,(image1.size.height - image2.size.height)/2, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

上一篇 下一篇

猜你喜欢

热点阅读