图片处理

1、 合成图片

/// 合成图片
/// @param images 需要合成图片的图片数组
/// @param overlapWidth 两个图片的重叠宽度
/// @param iconWidth 每个图片的宽度
/// @param iconHeight 每个图片的高度
/// 图片合成
+ (UIImage *)composeWithImages:(NSArray*)images
                  overlapWidth:(CGFloat)overlapWidth
                     imageWidth:(CGFloat)imageWidth
                    imageHeight:(CGFloat)imageHeight
{
    if (images.count <= 0) {
        return nil;
    }
    CGFloat resultImgWidth = (imageWidth + (images.count-1)*(imageWidth-overlapWidth));
    UIGraphicsBeginImageContext(CGSizeMake(resultImgWidth, imageHeight));//创建上下文
    for (int i = 0; i < images.count; i++) {
        UIImage *originImage = [images wbt_unknownObjectAtIndex:i];
//        UIImage *newImage = [self imageWithBorderW:5 borderColor:[UIColor whiteColor] image:originImage];
        CGFloat imgX = resultImgWidth-imageWidth-i*(imageWidth-overlapWidth);
//        [newImage drawInRect:CGRectMake(imgX, 0, imageWidth, imageHeight)];
        [originImage drawInRect:CGRectMake(imgX, 0, imageWidth, imageHeight)];
    }
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
    UIGraphicsEndImageContext();//关闭上下文
    return resultImg;
}

2、 绘制圆角边框图片

/// 绘制有圆角边框的图片
+ (UIImage *)imageWithBorderW:(CGFloat)borderW borderColor:(UIColor *)color image:(UIImage *)image {

    CGRect rect = [self _getImageRect:image];
    // 1.开启一个和原始图片一样大小的位图上下文.
    CGSize size = rect.size;
    UIGraphicsBeginImageContextWithOptions(size,NO,0);
    // 2.绘制一个大圆,填充
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [color set];
    [path fill];
    // 3.添加一个裁剪区域.
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, size.width-2*borderW, size.height-2*borderW)];
    [path addClip];
    // 4.把图片绘制到裁剪区域当中.
    [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
    // 5.生成一张新图片.
    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
    // 6.关闭上下文.
    UIGraphicsEndImageContext();
    return clipImage;
}


+ (CGRect)_getImageRect:(UIImage *)tempImage {
    CGRect rect;
    if (tempImage.size.width > tempImage.size.height) {
        rect = CGRectMake((tempImage.size.width-tempImage.size.height)/2, 0, tempImage.size.height, tempImage.size.height);
    } else if (tempImage.size.width < tempImage.size.height) {
        rect = CGRectMake(0, (tempImage.size.height-tempImage.size.width)/2, tempImage.size.width, tempImage.size.width);
    } else {
        rect = CGRectMake(0, 0, tempImage.size.width, tempImage.size.width);
    }
    return rect;
}

3、递归下载图片

/// 递归下载,全部成功为止
/// @param imageUrls url数组
/// @param images 请求到的image
/// @param index 初始值
/// @param success 成功回调
/// @param failure 失败回调
+ (void)donloadImage:(NSArray *)imageUrls images:(NSMutableArray *)images currentIndex:(NSUInteger)index success:(void (^)(NSArray*images))success failure:(void (^)())failure{
    __block NSUInteger currentIndex = index;
    __weak typeof(self) weakSelf = self;
    id webImage = [[WBS2Extensions defaultExtensions] instanceWithProtocol:@protocol(WBS2ImageExtensionProtocol)];
    [webImage loadImage:[imageUrls wbt_unknownObjectAtIndex:currentIndex] complete:^(id request, UIImage *image, NSData *data, NSError *error) {
        __strong typeof(self) strongSelf = weakSelf;
        currentIndex++;
        if (image && !error) {
            //图片下载成功
            [images addObject:image];
            //NSLog(@"下载成功!:%ld",currentIndex);
        }else{
            //NSLog(@"下载失败!:%ld error:%@",currentIndex,error);
        }
        //如果currentIndex == imageUrls.count 则停止递归并返回
        if(currentIndex == imageUrls.count){
            if(images.count>0){
                if(success){
                    success([images copy]);
                }
            }else{
                if(failure){
                    failure();
                }
            }
            return ;
        }else{
            //继续递归下载...并将参数传递下去,不然成功与失败时的Boloc无法正常回调
            [strongSelf donloadImage:imageUrls images:images currentIndex:currentIndex success:success failure:failure];
        }
    }];
}

4、批量下载,顺序返回image

/// 批量下载,顺序返回image
/// @param imgsArray url数组
/// @param completionBlock 顺序的image数组
+ (void)downloadImages:(NSArray *)imgsArray completion:(void(^)(NSArray *resultArray))completionBlock{
    id webImage = [[WBS2Extensions defaultExtensions] instanceWithProtocol:@protocol(WBS2ImageExtensionProtocol)];
    __block NSMutableDictionary *resultDict = [NSMutableDictionary new];

    for (int i = 0; i < imgsArray.count; i++) {
        NSString *url = [imgsArray wbt_unknownObjectAtIndex:i];
        [webImage loadImage:url complete:^(id request, UIImage *image, NSData *data, NSError *error) {
            if(error){
                //在对应的位置放一个error对象
                [resultDict setObject:[UIImage imageNamed:@"[email protected]"] forKey:@(i)];
            }else{
                [resultDict setObject:image forKey:@(i)];
            }
            NSLog(@"wyw----i----%d",i);
            if(resultDict.count == imgsArray.count) {
                //全部下载完成
                NSArray *resultArray = [self createDownloadResultArray:resultDict count:imgsArray.count];
                if(completionBlock){
                    completionBlock(resultArray);
                }
            }
        }];
    }
}

+ (NSArray *)createDownloadResultArray:(NSDictionary *)dict count:(NSInteger)count {
    NSMutableArray *resultArray = [NSMutableArray new];
    for(int i=0;i

你可能感兴趣的:(图片处理)