demo:https://download.csdn.net/download/u012881779/10933922
TO:YYWebImage
之所以有处理图片这需求,是因为我们后端返回的存在阿里云上的图片是原图,测试人员反映列表中图片加载很缓慢。
上传到阿里云OSS的图片,阿里云提供了一些功能。这里我只用了图片缩放和图片旋转。
缩放和旋转也可以同时处理,如:"?x-oss-process=image/resize,m_fill,h_120,w_120/rotate,90"。
#import "GADealViewController.h"
#import "YYWebImage.h"
@interface GADealViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *oneIV;
@property (weak, nonatomic) IBOutlet UIImageView *twoIV;
@property (weak, nonatomic) IBOutlet UIImageView *thrIV;
@property (weak, nonatomic) IBOutlet UIImageView *fouIV;
@end
@implementation GADealViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
链接不支持https协议时,需要去Info.plist设置允许任意加载
App Transport Security Settings
Allow Arbitrary Loads YES
*/
NSString *imgPath = @"http://lyzbimage.jbxgo.com/lyzbjbxgo/shop/4849/images/2019/01/04/15465716555417.jpg";
NSURL *oneURL = [NSURL URLWithString:imgPath];
[_oneIV setContentMode:UIViewContentModeScaleToFill];
[_oneIV yy_setImageWithURL:oneURL placeholder:nil];
NSURL *twoURL = [NSURL URLWithString:imgPath];
[_twoIV setContentMode:UIViewContentModeScaleAspectFit];
[_twoIV yy_setImageWithURL:twoURL placeholder:nil];
NSString *thrImgPath = [self dealResizeWithUrlString:imgPath withWidth:120*2 height:120*2];
NSURL *thrURL = [NSURL URLWithString:thrImgPath];
[_thrIV yy_setImageWithURL:thrURL placeholder:nil];
NSString *fouImgPath = [self dealRotateWithUrlString:imgPath WithValue:90];
NSURL *fouURL = [NSURL URLWithString:fouImgPath];
[_fouIV yy_setImageWithURL:fouURL placeholder:nil];
}
/*
图片缩放
https://help.aliyun.com/document_detail/44688.html?spm=a2c4g.11186623.6.1179.4d1f53b3WU0oXy
*/
- (NSString *)dealResizeWithUrlString:(NSString *)urlString withWidth:(NSInteger)width height:(NSInteger)height {
if (![urlString isKindOfClass:NSString.class]) {
return @"";
}
if (![urlString hasSuffix:@".gif"]) {
if ([urlString containsString:@"x-oss-process"]) { // 后台处理了
} else { // 前端自己处理
/*
指定缩略的模式:
lfit:等比缩放,限制在指定w与h的矩形内的最大图片。
mfit:等比缩放,延伸出指定w与h的矩形框外的最小图片。
fill:固定宽高,将延伸出指定w与h的矩形框外的最小图片进行居中裁剪。
pad: 固定宽高,缩略填充。
fixed:固定宽高,强制缩略。
*/
NSString *appendStr = [NSString stringWithFormat:@"?x-oss-process=image/resize,m_fill,h_%ld,w_%ld",width,height];
urlString = [urlString stringByAppendingString:appendStr];
}
}
return urlString;
}
/*
图片旋转
value 图片按顺时针旋转的角度,范围[0, 360],默认值为0表示不旋转。
https://help.aliyun.com/document_detail/44690.html?spm=a2c4g.11186623.6.1189.603f4ecdvDhXIg
*/
- (NSString *)dealRotateWithUrlString:(NSString *)urlString WithValue:(NSInteger)value {
if (![urlString isKindOfClass:NSString.class]) {
return @"";
}
if (![urlString hasSuffix:@".gif"]) {
if ([urlString containsString:@"x-oss-process"]) {
} else {
NSString *appendStr = [NSString stringWithFormat:@"?x-oss-process=image/rotate,%ld",(long)value];
urlString = [urlString stringByAppendingString:appendStr];
}
}
return urlString;
}
@end
示意图: