• 【代码笔记】iOS-多张图片合成一张


    代码:

     

    RootViewController.m

     

    复制代码
    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        NSArray *imgArray = [[NSArray alloc] initWithObjects:
                             [UIImage imageNamed:@"1.jpg"],
                             [UIImage imageNamed:@"2.jpg"],
                             [UIImage imageNamed:@"3.jpg"],
                             [UIImage imageNamed:@"4.jpg"],
                             [UIImage imageNamed:@"5.jpg"],
                            nil];
        
        NSArray *imgPointArray = [[NSArray alloc] initWithObjects:
                                  @"10", @"10",
                                  @"10", @"25",
                                  @"30", @"15",
                                  @"30", @"50",
                                  @"20", @"80",
                                  nil];
        
        
        BOOL suc = [self mergedImageOnMainImage:[UIImage imageNamed:@"1.jpg"] WithImageArray:imgArray AndImagePointArray:imgPointArray];
        
        if (suc == YES) { 
            NSLog(@"Images Successfully Mearged & Saved to Album"); 
        } 
        else { 
            NSLog(@"Images not Mearged & not Saved to Album"); 
        }
        
    }
    #pragma -mark -functions
    //多张图片合成一张
    - (BOOL) mergedImageOnMainImage:(UIImage *)mainImg WithImageArray:(NSArray *)imgArray AndImagePointArray:(NSArray *)imgPointArray
    {
        
        UIGraphicsBeginImageContext(mainImg.size);
        
        [mainImg drawInRect:CGRectMake(0, 0, mainImg.size.width, mainImg.size.height)];
        int i = 0;
        for (UIImage *img in imgArray) {
            [img drawInRect:CGRectMake([[imgPointArray objectAtIndex:i] floatValue],
                                       [[imgPointArray objectAtIndex:i+1] floatValue],
                                       img.size.width,
                                       img.size.height)];
            
            i+=2;
        }
        
        CGImageRef NewMergeImg = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage,
                                                              CGRectMake(0, 0, mainImg.size.width, mainImg.size.height));
        
        UIGraphicsEndImageContext();
        if (NewMergeImg == nil) {
            return NO;
        }
        else {
            UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:NewMergeImg], self, nil, nil);
            return YES;
        }
    }
    
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    复制代码

     

  • 相关阅读:
    低调 、隐忍、善良应是最应该修炼的
    达内C++培训课程
    这三天低效率开发的总结,我都做了些什么啊?
    linux sysfs(1)
    编码问题
    Linux中的system函数的实现和解释
    北京邮电大学 程序设计课程设计 电梯 文件输入版本(已调试,大致正确运行==)
    "Dallas" CTP3 发布通告
    结合使用PowerPivot 和 "Dallas" CTP3
    Windows Azure 解决方案系列: 能源监测减少支出,通过托管平台拓展业务
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/5197099.html
Copyright © 2020-2023  润新知