之前有一个小的需求,分享一张长图。于是琢磨了一下做了一个类别,分享一下方法:
@implementation UIImage (CompossImages)
+ (UIImage *)composeImages:(NSArray<UIImage *> *)arr{
//间隔
int margin = 20;
//左右间距
int marginL = 15;
//合成宽度
CGFloat width = 2*[UIScreen mainScreen].bounds.size.width - 2*marginL;
//合成高度
CGFloat height = 0;
//计算高度
for (UIImage *img in arr) {
CGFloat h = img.size.height / (img.size.width/width) + margin;
height += h;
}
//画板大小
UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width*2, height+margin+10));
//当前y值
CGFloat y = 20;
//画图
for (int i = 0; i<arr.count;i++) {
UIImage *img = arr[i];
CGFloat w = img.size.width;
CGFloat h = img.size.height;
[img drawInRect:CGRectMake(marginL, y, width, h / (w/width))];
y += h / (w/width) +margin ;
}
CGFloat x = 20;
CGFloat imgW = x/52.f*428;
//水印
[[UIImage imageNamed:@"share_shuiyin"] drawInRect:CGRectMake([UIScreen mainScreen].bounds.size.width*2-imgW-marginL, height+5, imgW, x)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
@end
可能会有点吃资源,如果有更好的方法,望网友分享