iOS 合成图片(二)

直接上代码

如要参考,请先看懂再参考 代码中的逻辑可能只满足我们的需求。如有疑问:我是磊怀 请你联系我2849765859qq或参考我上篇博客

效果图如下 微信头像和微信昵称,还有下面二维码的位置,都是合成上去的,整体展示出来只是一个image而已
在这里插入图片描述

//
//  XLImageSynthesis.h
//  mahjonghn
//
//  Created by 磊怀王 on 2019/2/20.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface XLImageSynthesis : NSObject


/**
 给已知的图片添加水印图片

 @param originImage 原图
 @param waterImage 水印图片
 @param waterRect 水印图片的位置,尺寸
 @return 返回合成后的图片
 */
+ (UIImage *)XLAddWaterImage:(UIImage *)originImage
                  waterImage:(UIImage *)waterImage
                loactionRect:(CGRect)waterRect;


/**
 给image切圆角

 @param originImage 原始图片
 @return 返回新图片
 */
+ (UIImage *)XLCircleImage:(UIImage * _Nullable)originImage;



/**
 合成 文本

 @param originalImage 原始图片
 @param targetText 目标文本
 @param rect 文本位置
 @return 返回新图片
 */
+ (UIImage *)XLAddTextImage:(UIImage *)originalImage
                       text:(NSString *)targetText
                   textRect:(CGPoint)rect;



/**
 得到最终的合成的图片

 @param qrcode 二维码链接
 @param completeBlock 合成完毕
 @param bottomImage 底图
 */
+ (void)XLSynthesisIconNicknameQRCode:(NSString * _Nullable)qrcode
                          bottomImage:(UIImage *)bottomeImage
                             complete:(void(^)(UIImage * finishImage))completeBlock;

@end

NS_ASSUME_NONNULL_END

//
//  XLImageSynthesis.m
//  mahjonghn
//
//  Created by 磊怀王 on 2019/2/20.
//

#import "XLImageSynthesis.h"
#import "XLDownLoadImages.h"

@implementation XLImageSynthesis


+ (UIImage *)XLAddWaterImage:(UIImage *)originImage waterImage:(UIImage *)waterImage loactionRect:(CGRect)waterRect{
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(originImage.size, NO, 0);
    //将原图加在画布上
    [originImage drawInRect:CGRectMake(0, 0, originImage.size.width, originImage.size.height)];
    //将水印图片加在画布上
    [waterImage drawInRect:waterRect];
    //合成图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭画布
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)XLCircleImage:(UIImage * _Nullable)originImage{
    //1、开启上下文
    UIGraphicsBeginImageContextWithOptions(originImage.size, NO, 0);
    
    //2、设置裁剪区域
    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, originImage.size.width, originImage.size.height)];
    [path addClip];
    
    //3、绘制图片
    [originImage drawAtPoint:CGPointZero];
    
    //4、获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //5、关闭上下文
    UIGraphicsEndImageContext();
    
    //6、返回新图片
    return newImage;
}

+ (UIImage *)XLAddTextImage:(UIImage *)originalImage text:(NSString *)targetText textRect:(CGPoint)point{
    UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 0);
    [originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
    [targetText drawAtPoint:point withAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:28]}];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

+ (void)XLSynthesisIconNicknameQRCode:(NSString * _Nullable)qrcode bottomImage:(UIImage *)bottomeImage complete:(void(^)(UIImage * finishImage))completeBlock{
    __strong UIImage * imageScreen = bottomeImage;
    NSString * iconUrl = [XLClient shared].userManager.activedUser.avatar;
    if (!isEmpty(iconUrl)) {
        [XLDownLoadImages downLoadOneImage:iconUrl complete:^(UIImage * _Nullable endImage, NSError * _Nullable error) {
            if (error == nil) {
                //图像切圆角
                UIImage * circleImage = [XLImageSynthesis XLCircleImage:endImage];
                //把头像合成上去
                UIImage * withIconImage = [XLImageSynthesis XLAddWaterImage:imageScreen waterImage:circleImage loactionRect:CGRectMake(250, 32, 68, 68)];
                NSString * userNickname = [XLClient shared].userManager.activedUser.nickName;
                if (!isEmpty(userNickname)) {
                    //把昵称合成上去
                    UIImage * withTextImage = [XLImageSynthesis XLAddTextImage:withIconImage text:userNickname textRect:CGPointMake(346, 47)];
                    [XLDownLoadImages downLoadOneImage:qrcode complete:^(UIImage * _Nullable endImage, NSError * _Nullable error) {
                        if (error == nil) {
                            UIImage * qrCodeImage = [XLImageSynthesis XLAddWaterImage:withTextImage waterImage:endImage loactionRect:CGRectMake(236, 878, 280, 280)];
                            completeBlock(qrCodeImage);
                        }
                    }];
                    
                    
                }
                
            }
        }];
    }
}


@end

这其中涉及了一些图片下载的方法 ,会用到如下类

//
//  XLDownLoadImages.h
//  mahjonghn
//
//  Created by 磊怀王 on 2019/2/19.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface XLDownLoadImages : NSObject


/**
 下载单张图片

 @param imageUrl 要下载的图片
 @param completeBlock 下载完成
 */
+ (void)downLoadOneImage:(NSString *)imageUrl
                complete:(void(^)(UIImage * _Nullable endImage,NSError * _Nullable error))completeBlock;

@end

NS_ASSUME_NONNULL_END

//
//  XLDownLoadImages.m
//  mahjonghn
//
//  Created by 磊怀王 on 2019/2/19.
//

#import "XLDownLoadImages.h"
#import "SDWebImageManager.h"


@implementation XLDownLoadImages

+ (void)downLoadImages:(NSArray <NSString *> *)imageArr downCompletion:(void(^)(NSDictionary * resultDic))completeBlock{
    if (imageArr.count == 0) {
        NSLog(@"没有要下载的链接");
        return;
    }
    SDWebImageDownloader * manager = [SDWebImageDownloader sharedDownloader];
    manager.downloadTimeout = 30;
    manager.maxConcurrentDownloads = 10;
    
    __block NSMutableDictionary * resultDic = [[NSMutableDictionary alloc] init];
    for (int i = 0 ; i < imageArr.count; i++) {
        NSString * imageUrl = [imageArr objectAtIndex:i];
        [manager downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache | SDWebImageDownloaderScaleDownLargeImages progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if (finished == YES) {
                if (error == nil) {
                    [resultDic setObject:image forKey:imageArr[i]];
                }else{
                    [resultDic setObject:error forKey:imageArr[i]];
                }
            }
            
            if (resultDic.count == imageArr.count) {
                if (completeBlock) {
                    completeBlock(resultDic);
                }
            }
        }];
    }
}

+ (void)downLoadOneImage:(NSString *)imageUrl complete:(void(^)(UIImage * _Nullable endImage,NSError * _Nullable error))completeBlock{
    SDWebImageDownloader * manager = [SDWebImageDownloader sharedDownloader];
    [manager downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache | SDWebImageDownloaderScaleDownLargeImages progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        if (finished == YES) {
            completeBlock(image,error);
        }else{
            NSLog(@"图片下载失败");
        }
        
    }];
}

@end

猜你喜欢

转载自blog.csdn.net/weixin_43883776/article/details/87813323