[iOS]上传到阿里云OSS的图片处理以及视频获取第一帧图片

demo:https://download.csdn.net/download/u012881779/10933922
TO:YYWebImage
之所以有处理图片这需求,是因为我们后端返回的存在阿里云上的图片是原图,测试人员反映列表中图片加载很缓慢。
上传到阿里云OSS的图片,阿里云提供了一些功能。这里我只用了图片缩放图片旋转
缩放和旋转也可以同时处理,如:"?x-oss-process=image/resize,m_fill,h_120,w_120/rotate,90"。

获取视频第一帧图片
https://lyzbjbx.oss-cn-hangzhou.aliyuncs.com/lyzblbs/client/2019/04/19/sc7ft5Zrqh.mp4?x-oss-process=video/snapshot,t_10000,m_fast

#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

示意图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值