封装七牛上传SDK

七牛上传文件

现在越来越多的项目会使用云存储,一些图片视频资源不会再放到自己的服务器中。目前市场上用的最多的应该是阿里云、腾讯云 、七牛云吧。今天我们主要讲的是七牛云。七牛官方文档

看了官方文档后,我们首先要知道为了安全,我们上传东西所需要的秘钥肯定是通过服务端获取,不可能是放在自己本地的。为了使用方便,我把七牛上传封装成一个简单的单例工具类。

七牛的上传是自己生成上传的key ,上传成功后,会返回你上传的key 以及一个hash值。你只需要将key 与 在七牛后台设置的资源server 拼接起来,就可以拿到资源了。 建议拼接这个事,最好让后台进行,说不定server会改变

@interface FFQiNiuApiManager : NSObject

/**
 七牛上传所需的token   服务端获取
 */
@property (nonatomic,copy)  NSString  *upToken;

/**
 七牛前面需要拼接的server
 */
@property (nonatomic , copy)  NSString  *qiniuServerUrl;

@property (nonatomic , strong)  QNUploadManager *upManager ;
/**
 七牛获取token 成功
 */
@property (nonatomic, copy) void (^getTokenSuccessBlock)();

+(FFQiNiuApiManager *)shareManager;




/**
 上传文件

 @param filePath 文件z地址
 @param key 图片的key
 @param upFinsh 上传成功
 @param failInfo 失败
 @param upProgress 上传进度
 */
- (void)uploadWithFilePath:(NSString *)filePath andUpKey:(NSString *)key andUpFail:(void (^)(QNResponseInfo *))failInfo andUpSuccess:(void(^)(NSDictionary *infoDict))upFinsh andUpLoadProgress:(void(^)(float progress))upProgress;
/**
 上传单个图片
 
 @param key 图片的key
 @param upImage 上传的图片
 @param upFinsh 上传成功
 @param failInfo 失败
 @param upProgress 上传进度
 */
- (void)upLoadImageWithUploadKey:(NSString *)key andImage:(UIImage *)upImage andFinshInfo:(void(^)(NSDictionary *infoDict))upFinsh andUpFail:(void (^)(QNResponseInfo *))failInfo andUpLoadProgress:(void(^)(float progress))upProgress;



/**
 上传图片或视频
 
 @param phAsset 上传的素材
 @param key 上传到七牛所需要的key
 @param upFinsh 上传成功的回调
 @param failInfo 上传失败的信息   具体看 QNResponseInfo 里面的属性
 @param upProgress 上传进度
 */
- (void)upLoadPHAsset:(PHAsset *)phAsset andUpKey:(NSString *)key andUpFail:(void (^)(QNResponseInfo *))failInfo andUpSuccess:(void(^)(NSDictionary *infoDict))upFinsh andUpLoadProgress:(void(^)(float progress))upProgress;

- (void)getQiNiuToken;

/**
 生成图片上传key

 @return
 */
+ (NSString *)getImageKey;

/**
 生成视频上传key
 
 @return
 */
+ (NSString *)getVideoKey;

/**
 生成音频上传key
 
 @return
 */
+ (NSString *)getVoiceKey;

+(FFQiNiuApiManager *)shareManager{
    static FFQiNiuApiManager *sharedGlobalInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedGlobalInstance = [[self alloc] init];
    });
    return sharedGlobalInstance;
}

-(id)init{
    self=[super init];
    if (self) {
        self.upManager = [[QNUploadManager alloc] init];
    }
    return self;
}

- (void)getQiNiuToken {
    //这里写七牛获取token 的数据请求  成功后,用block 回调
}

- (void)uploadWithFilePath:(NSString *)filePath andUpKey:(NSString *)key andUpFail:(void (^)(QNResponseInfo *))failInfo andUpSuccess:(void(^)(NSDictionary *infoDict))upFinsh andUpLoadProgress:(void(^)(float progress))upProgress {
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        if (upProgress) {
            upProgress(percent);
        }
        if (percent >= 1){
            NSLog(@"上传成功");
        }
        
    }params:nil checkCrc:NO cancellationSignal:nil];
    [self.upManager putFile:filePath key:key token:self.upToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        if(info.statusCode == 200){
            if (upFinsh) {
                upFinsh(resp);
            }
        }else {
            if(failInfo){
                failInfo(info);
            }
        }
    } option:uploadOption];
}


- (void)upLoadPHAsset:(PHAsset *)phAsset andUpKey:(NSString *)key andUpFail:(void (^)(QNResponseInfo *))failInfo andUpSuccess:(void(^)(NSDictionary *infoDict))upFinsh andUpLoadProgress:(void(^)(float progress))upProgress {
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        if (upProgress) {
            upProgress(percent);
        }
        if (percent >= 1){
            NSLog(@"上传成功");
        }
        
    }params:nil checkCrc:NO cancellationSignal:nil];
    
    [self.upManager  putPHAsset:phAsset key:key token:self.upToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        if(info.statusCode == 200){
            if (upFinsh) {
                upFinsh(resp);
            }
        }else {
            if(failInfo){
                failInfo(info);
            }
        }
    } option:uploadOption];
    
}


- (void)upLoadImageWithUploadKey:(NSString *)key andImage:(UIImage *)upImage andFinshInfo:(void (^)(NSDictionary *))upFinsh andUpFail:(void (^)(QNResponseInfo *))failInfo andUpLoadProgress:(void (^)(float))upProgress {

    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
        NSLog(@"percent == %.2f", percent);
        if (upProgress) {
            upProgress(percent);
        }
    } params:nil checkCrc:NO cancellationSignal:nil];
    NSData *data;
    
    if (UIImagePNGRepresentation(upImage) == nil) {
        data = UIImageJPEGRepresentation(upImage, 1.0);
    } else {
        data = UIImagePNGRepresentation(upImage);
    }
    [self.upManager putData:data key:key token:self.upToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        NSLog(@"info ===== %@", info);
        NSLog(@"resp ===== %@", resp);
        if(info.statusCode == 200){
            if (upFinsh) {
                upFinsh(resp);
            }
        }else {
            if(failInfo){
                failInfo(info);
            }
        }
    } option:uploadOption];
    
}

- (NSData *)compressQualityWithMaxLength:(NSInteger)maxLength andImage:(UIImage *)image {
    CGFloat compression = 1;
    maxLength = 1.5*1024*1024;
    NSData *data = UIImageJPEGRepresentation(self, compression);
    if (data.length < maxLength) return data;
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(self, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    return data;
}


+ (NSString *)getImageKey {
    NSString *num = [NSString stringWithFormat:@"%.4d", (arc4random() % 10000)];
    NSString *name = [NSString stringWithFormat:@"%@%@",[MYHelper getTime],num];
    //设置上传key
    NSString *upKey = [NSString stringWithFormat:@"fifty/image/fifty%@",name];
    return upKey;
}


+ (NSString *)getVideoKey {
    NSString *num = [NSString stringWithFormat:@"%.4d", (arc4random() % 10000)];
    NSString *name = [NSString stringWithFormat:@"%@%@",[MYHelper getTime],num];
    //设置上传key
    NSString *upKey = [NSString stringWithFormat:@"fifty/video/fifty%@",name];
    return upKey;
}

+ (NSString *)getVoiceKey {
    NSString *num = [NSString stringWithFormat:@"%.4d", (arc4random() % 10000)];
    NSString *name = [NSString stringWithFormat:@"%@%@",[MYHelper getTime],num];
    //设置上传key
    NSString *upKey = [NSString stringWithFormat:@"fifty/vioce/fifty%@",name];
    return upKey;
}

上传这些东西都很简单。封装成工具类,方便调用,统一处理,非常方便。视频音频上传路径,图片可以直接上传。

你可能感兴趣的:(封装七牛上传SDK)