ios两个图片合成为一张

UIImage *img = [UIImage imageNamed:@"Group 3"];

    CGImageRef imgRef = img.CGImage;

    CGFloat w = CGImageGetWidth(imgRef);

    CGFloat h = CGImageGetHeight(imgRef);

    

    //以1.png的图大小为底图

    UIImage *img1 = [UIImage imageNamed:@"Group 48"];

    CGImageRef imgRef1 = img1.CGImage;

    CGFloat w1 = CGImageGetWidth(imgRef1);

    CGFloat h1 = CGImageGetHeight(imgRef1);

    

    //以整个屏幕大小为上下文

    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height));

    [img1 drawInRect:CGRectMake(0, 0, w1, h1)];//先把1.png 画到上下文中

    [img drawInRect:CGRectMake(0, h1, w, h)];//再把小图放在上下文中

    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片

    UIGraphicsEndImageContext();//关闭上下文

    

    

    UIImageView *imageaa = [[UIImageView alloc] initWithFrame:self.view.frame];

    imageaa.image = resultImg;

    [self.view addSubview:imageaa];

    

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filePath = [path stringByAppendingPathComponent:@"01.png"];

    [UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存图片到沙盒

    NSLog(@"%@", filePath);

    CGImageRelease(imgRef);

    CGImageRelease(imgRef1);

你可能感兴趣的:(ios两个图片合成为一张)