Object-C UIImage 合并多张图片

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods


一.UIImage 合并多张图片

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C UIImage 合并多张图片
//@Time:2021/10/10 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
#pragma mark - 合并 UIImage
/**
* return 合成后的图片 (以坐标为参考点,准确)
* @param mainImage 第一张图片位画布 (必传,不可空)
* @param viewFrame 第一张图片所在View的frame(获取压缩比用) (必传,不可空)
* @param imgArray 子图片数组 (必传,不可空)
* @param frameArray 子图片坐标数组 (必传,不可空)
*/
-(UIImage *)mergeImageOnMainImage:(UIImage *)mainImage
mainImageViewFrame:(CGRect)viewFrame
subImageArray:(NSArray *)imgArray
subImageFrameArray:(NSArray *)frameArray {
if (!mainImage) { return nil; }
if (viewFrame.size.width == 0 || viewFrame.size.height == 0) { return nil; }
if (imgArray.count == 0) { return nil; }
if (imgArray.count != frameArray.count) { return nil; }
// 此处拿到缩放比例
CGFloat widthScale = mainImage.size.width / viewFrame.size.width;
CGFloat heightScale = mainImage.size.height / viewFrame.size.height;
UIGraphicsBeginImageContext(CGSizeMake(mainImage.size.width, mainImage.size.height));
[mainImage drawInRect:CGRectMake(0, 0, mainImage.size.width, mainImage.size.height)];
int i = 0;
for (UIImage *img in imgArray) {
CGRect fristRect = [[frameArray objectAtIndex:i] CGRectValue];
[img drawInRect:CGRectMake(fristRect.origin.x * widthScale, fristRect.origin.y * heightScale, fristRect.size.width, fristRect.size.height)];
i+=1;
}
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImg == nil ? mainImage : resultImg;
}
- (void)viewDidLoad {
[super viewDidLoad];
//底图
UIImage* mainImage = [UIImage imageNamed:@"123.png"];
CGRect mainRect = CGRectMake(0, 0, mainImage.size.width, mainImage.size.height);
//需要合并的图片
NSMutableArray* subImages = [[NSMutableArray alloc] init];
UIImage* subImage = [UIImage imageNamed:@"456.png"];
[subImages addObject:subImage];
//设置合并图片的显示位置
NSMutableArray* subImageRects = [[NSMutableArray alloc] init];
CGRect rt = CGRectMake(0, 0, subImage.size.height/2.0, subImage.size.height/2.0);
[subImageRects addObject:[NSValue valueWithCGRect:rt]];
UIImage* dstImage = [self mergeImageOnMainImage:mainImage mainImageViewFrame:mainRect subImageArray:subImages subImageFrameArray:subImageRects];
}
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C UIImage 合并多张图片
//@Time:2021/10/10 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/


#pragma mark - 合并 UIImage
/**
 *  return 合成后的图片 (以坐标为参考点,准确)
 *  @param mainImage        第一张图片位画布                          (必传,不可空)
 *  @param viewFrame        第一张图片所在View的frame(获取压缩比用)    (必传,不可空)
 *  @param imgArray         子图片数组                               (必传,不可空)
 *  @param frameArray       子图片坐标数组                            (必传,不可空)
 */

-(UIImage *)mergeImageOnMainImage:(UIImage *)mainImage
                 mainImageViewFrame:(CGRect)viewFrame
                      subImageArray:(NSArray *)imgArray
                 subImageFrameArray:(NSArray *)frameArray {
   if (!mainImage) {   return nil; }
   if (viewFrame.size.width == 0 || viewFrame.size.height == 0) {   return nil; }
   if (imgArray.count == 0) {  return nil;  }
   if (imgArray.count != frameArray.count) {  return nil;  }
   
   // 此处拿到缩放比例
   CGFloat widthScale = mainImage.size.width / viewFrame.size.width;
   CGFloat heightScale = mainImage.size.height / viewFrame.size.height;

   UIGraphicsBeginImageContext(CGSizeMake(mainImage.size.width, mainImage.size.height));
   [mainImage drawInRect:CGRectMake(0, 0, mainImage.size.width, mainImage.size.height)];
   int i = 0;
   for (UIImage *img in imgArray) {
       CGRect fristRect = [[frameArray objectAtIndex:i] CGRectValue];
       [img drawInRect:CGRectMake(fristRect.origin.x * widthScale, fristRect.origin.y * heightScale, fristRect.size.width, fristRect.size.height)];
       i+=1;
   }
   
   UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   
       return resultImg == nil ? mainImage : resultImg;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //底图
    UIImage* mainImage = [UIImage imageNamed:@"123.png"];
    CGRect mainRect = CGRectMake(0, 0, mainImage.size.width, mainImage.size.height);
    
    //需要合并的图片
    NSMutableArray* subImages = [[NSMutableArray alloc] init];
    UIImage* subImage = [UIImage imageNamed:@"456.png"];
    [subImages addObject:subImage];
    
    //设置合并图片的显示位置
    NSMutableArray* subImageRects = [[NSMutableArray alloc] init];
    CGRect rt = CGRectMake(0, 0, subImage.size.height/2.0, subImage.size.height/2.0);
    [subImageRects addObject:[NSValue valueWithCGRect:rt]];
    
    
    UIImage* dstImage = [self mergeImageOnMainImage:mainImage mainImageViewFrame:mainRect subImageArray:subImages subImageFrameArray:subImageRects];
}
/******************************************************************************************/ //@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:Object-C UIImage 合并多张图片 //@Time:2021/10/10 08:00 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! /******************************************************************************************/ #pragma mark - 合并 UIImage /** * return 合成后的图片 (以坐标为参考点,准确) * @param mainImage 第一张图片位画布 (必传,不可空) * @param viewFrame 第一张图片所在View的frame(获取压缩比用) (必传,不可空) * @param imgArray 子图片数组 (必传,不可空) * @param frameArray 子图片坐标数组 (必传,不可空) */ -(UIImage *)mergeImageOnMainImage:(UIImage *)mainImage mainImageViewFrame:(CGRect)viewFrame subImageArray:(NSArray *)imgArray subImageFrameArray:(NSArray *)frameArray { if (!mainImage) { return nil; } if (viewFrame.size.width == 0 || viewFrame.size.height == 0) { return nil; } if (imgArray.count == 0) { return nil; } if (imgArray.count != frameArray.count) { return nil; } // 此处拿到缩放比例 CGFloat widthScale = mainImage.size.width / viewFrame.size.width; CGFloat heightScale = mainImage.size.height / viewFrame.size.height; UIGraphicsBeginImageContext(CGSizeMake(mainImage.size.width, mainImage.size.height)); [mainImage drawInRect:CGRectMake(0, 0, mainImage.size.width, mainImage.size.height)]; int i = 0; for (UIImage *img in imgArray) { CGRect fristRect = [[frameArray objectAtIndex:i] CGRectValue]; [img drawInRect:CGRectMake(fristRect.origin.x * widthScale, fristRect.origin.y * heightScale, fristRect.size.width, fristRect.size.height)]; i+=1; } UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultImg == nil ? mainImage : resultImg; } - (void)viewDidLoad { [super viewDidLoad]; //底图 UIImage* mainImage = [UIImage imageNamed:@"123.png"]; CGRect mainRect = CGRectMake(0, 0, mainImage.size.width, mainImage.size.height); //需要合并的图片 NSMutableArray* subImages = [[NSMutableArray alloc] init]; UIImage* subImage = [UIImage imageNamed:@"456.png"]; [subImages addObject:subImage]; //设置合并图片的显示位置 NSMutableArray* subImageRects = [[NSMutableArray alloc] init]; CGRect rt = CGRectMake(0, 0, subImage.size.height/2.0, subImage.size.height/2.0); [subImageRects addObject:[NSValue valueWithCGRect:rt]]; UIImage* dstImage = [self mergeImageOnMainImage:mainImage mainImageViewFrame:mainRect subImageArray:subImages subImageFrameArray:subImageRects]; }

原图:

图片[1]-Object-C UIImage 合并多张图片-猿说编程
图片[2]-Object-C UIImage 合并多张图片-猿说编程

合并后:

图片[3]-Object-C UIImage 合并多张图片-猿说编程

二.猜你喜欢


ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
Hard-working is actually a cool thing.
努力学习其实是一件很酷的事
评论 抢沙发

请登录后发表评论

    暂无评论内容