项目想设置在图片上添加圆形图片,可获取不到imageView,实在没办法就在图片上画上图片
Demo:
// 在图片上添加图片;imageName 1.底部图片名字imageName, image2 需添加的图片
- (UIImage *)createShareImage:(UIImage *)tImage ContextImage:(UIImage *)image2
{
UIImage *sourceImage = tImage;
CGSize imageSize; //画的背景 大小
imageSize = [sourceImage size];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
//获得 图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//画 自己想要画的内容(添加的图片)
CGContextDrawPath(context, kCGPathStroke);
CGRect rect = CGRectMake( imageSize.width/4,imageSize.height/5, imageSize.width/2, imageSize.height/2);
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[image2 drawInRect:rect];
//返回绘制的新图形
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
// 1.将文字添加到图片上;imageName 图片名字, text 需画的字体
- (UIImage *)createShareImage:(UIImage *)tImage Context:(NSString *)text
{
UIImage *sourceImage = tImage;
CGSize imageSize; //画的背景 大小
imageSize = [sourceImage size];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
//获得 图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextDrawPath(context, kCGPathStroke);
CGFloat nameFont = 8.f;
//画 自己想要画的内容
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]};
CGRect sizeToFit = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, nameFont) options:NSStringDrawingUsesDeviceMetrics attributes:attributes context:nil];
NSLog(@"图片: %f %f",imageSize.width,imageSize.height);
NSLog(@"sizeToFit: %f %f",sizeToFit.size.width,sizeToFit.size.height);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
[text drawAtPoint:CGPointMake((imageSize.width-sizeToFit.size.width)/2,0) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:nameFont]}];
//返回绘制的新图形
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}