使用阿里云图片处理进行图片请求

本文使用了yywebimage作为图片请求库,我主要想分享的是后面的参数

直接上代码:

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 @param placeholderImage 占位图
 @param autoCut 是否处理自动剪切
 @param size frame size
 @param complete 回调
 */
- (void)mm_imageWithURLString:(NSString *)urlString placeholderImage:(UIImage *)placeholderImage autoCut:(BOOL)autoCut size:(CGSize)size complete:(void(^)(UIImage *image, NSURL *url))complete {
    NSInteger renderTimes = [MMUtils currentiPhoneRenderValue];
    
    NSString *imageUrl;
    if (urlString && [urlString isKindOfClass:[NSString class]] && urlString.length > 0) {
        if ([urlString containsString:@"XXXXX"]) {
            //图片来自于我司的阿里云域名
            NSString *str;
            if (size.width == 0 || size.height == 0 || !autoCut || [urlString containsString:@"x-oss-process=image"]) {
                str = urlString;
                imageUrl = str.encodingStr;
            } else {
                str = [NSString stringWithFormat:@"x-oss-process=image/resize,m_lfit,w_%zd,h_%zd/format,jpg", (NSInteger)size.width*renderTimes, (NSInteger)size.height*renderTimes];
                imageUrl = [NSString stringWithFormat:@"%@?%@", urlString.encodingStr, str];
            }
            DDLog(@"[阿里云]imageUrl : %@", imageUrl);
            
            [self yy_setImageWithURL:[NSURL URLWithString:imageUrl] placeholder:placeholderImage options:YYWebImageOptionProgressiveBlur completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                BLOCK_EXEC(complete, image, url)
            }];
        } else {
            //图片来自于第三方
            imageUrl = urlString.encodingStr;
            DDLog(@"[第三方]imageUrl : %@", imageUrl);
            
            [self yy_setImageWithURL:[NSURL URLWithString:imageUrl] placeholder:placeholderImage options:YYWebImageOptionProgressiveBlur completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                BLOCK_EXEC(complete, image, url)
            }];
        }
    } else {
        if (placeholderImage) {
            [self setImage:placeholderImage];
        }
    }
}

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 @param placeholderImage 占位图
 @param autoCut 是否处理自动剪切
 @param complete 回调
 */
- (void)mm_imageWithURLString:(NSString *)urlString placeholderImage:(UIImage *)placeholderImage autoCut:(BOOL)autoCut complete:(void(^)(UIImage *image, NSURL *url))complete {
    CGSize size = self.frame.size;
    [self mm_imageWithURLString:urlString placeholderImage:placeholderImage autoCut:autoCut size:size complete:complete];
}

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 @param placeholderImage 占位图
 @param complete 回调
 */
- (void)mm_imageWithURLString:(NSString *)urlString placeholderImage:(UIImage *)placeholderImage complete:(void(^)(UIImage *image, NSURL *url))complete {
    [self mm_imageWithURLString:urlString placeholderImage:placeholderImage autoCut:YES complete:complete];
}

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 @param placeholderImage 占位图
 */
- (void)mm_imageWithURLString:(NSString *)urlString placeholderImage:(UIImage *)placeholderImage {
    [self mm_imageWithURLString:urlString placeholderImage:placeholderImage complete:nil];
}

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 @param placeholderName 占位图名称
 */
- (void)mm_imageWithURLString:(NSString *)urlString placeholderName:(NSString *)placeholderName {
    [self mm_imageWithURLString:urlString placeholderImage:[UIImage imageNamed:placeholderName]];
}

/**
 根据图片地址获取图片
 
 @param urlString 地址字符串
 */
- (void)mm_imageWithURLString:(NSString *)urlString {
    [self mm_imageWithURLString:urlString placeholderImage:nil];
}

总结一下,参数是:

x-oss-process=image/resize,m_lfit,w_%zd,h_%zd/format,jpg

1、其中需要给出图片的size,通过获取控件的size,进行拼装

2、使用masonry时可能不能用self.frame.size获取尺寸,需要用参数获取

3、png可能存在透明通道,需要使用转换为jpg来用白色背景完善,否则透明区域会显示为黑色

4、本文是来自于UIImageView的一个类别,看官们可以自行封装

完事。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s