首页 > 代码库 > 图片、视频上传小记
图片、视频上传小记
我们开发中常用到图片上传,比如头像上传之类的,还有类似发布空间说说时发布多张图片等等,我在这里通过借鉴网络资源,并依赖于七牛云存储做了一个图片上传的小结。
我首先封装了一个图片上传的工具类,继承自nsobject,命名为QiniuTool;再多图上传时,还需要单张图片的上传结果处理,因此,我在这里有多创建了一个辅助工具类,命名为QiniuUploadHelper;
工具类类方法:
#import <Foundation/Foundation.h> //上传 视频 、n 张图片 #import <QiniuSDK.h> #import <UIKit/UIKit.h> typedef enum { type_image, type_video }LoadType; @interface QiniuTool : NSObject //获取七牛上传token + (void)getQiniuUploadToken:(LoadType)type loadUrl:(NSString *)tokenUrl result:(void(^)(NSDictionary *token))success failure:(void(^)())failure; /** 上传视频 @param videoPathUrl 视频的本地URL路径 */ //+ (void)uploadVedio:(NSURL *)videoPathUrl success:(void(^)(NSMutableDictionary *result))success failure:(void(^)())failure; /** 上传单张图片 @param image 图片 @param progress 上传进度 @param success 成功回调 @param failure 失败回调 */ + (void)uploadImage:(UIImage*)image loadUrl:(NSString *)tokenUrl progress:(QNUpProgressHandler)progress success:(void(^)(NSString*url))success failure:(void(^)())failure; //上传多张图片,按队列依次上传 + (void)uploadImages:(NSArray*)imageArray loadUrl:(NSString *)tokenUrl progress:(void(^)(CGFloat))progress success:(void(^)(NSArray*urlArray))success failure:(void(^)())failure; @end
实现过程:
#import "QiniuTool.h" #import "QiniuUploadHelper.h" #import "EBirdDefine.h" @implementation QiniuTool //获取七牛的token // 此处的networkrequest是我最封装的网络请求工具类 +(void)getQiniuUploadToken:(LoadType)type loadUrl:(NSString *)tokenUrl result:(void (^)(NSDictionary *))success failure:(void (^)())failure { [NetWorkRequest GET_Verify:BASEURL_(tokenUrl) parameters:nil hiddenLoading:NO success:^(NSInteger errorCode, id responseObject, NSString *errorMsg) { if (errorCode == 0 && responseObject) { success(responseObject); } } failure:^(NSError *error) { failure(); }]; } //上传单张图片 + (void)uploadImage:(UIImage *)image loadUrl:(NSString *)tokenUrl progress:(QNUpProgressHandler)progress success:(void (^)(NSString *))success failure:(void (^)())failure { [QiniuTool getQiniuUploadToken:type_image loadUrl:tokenUrl result:^(NSDictionary *token) { //此处block中的键值应根据后台设置,取出正确的结果 NSString *tokenStr = [token objectForKey:@"token"]; NSData*data =http://www.mamicode.com/UIImageJPEGRepresentation(image,0.01); if(!data) { if(failure) { failure(); } return; } NSString*fileName = [token objectForKey:@"key"]; BOOL isHttps = TRUE; QNZone * httpsZone = [[QNAutoZone alloc] initWithHttps:isHttps dns:nil]; QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) { builder.zone = httpsZone; }]; QNUploadOption *opt = [[QNUploadOption alloc]initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil]; QNUploadManager *uploadManager = [QNUploadManager sharedInstanceWithConfiguration:config]; [uploadManager putData:data key:fileName token:tokenStr complete:^(QNResponseInfo*info,NSString*key,NSDictionary*resp) { // 此处的结果应根据后台设置取出相对应的结果 if(info.statusCode == 200 && [resp count]) { //QiNiuBaseUrl NSString *url; if ([tokenUrl isEqualToString:@"/customers/headImageUploadToken"]) { url = [NSString stringWithFormat:@"%@",resp[@"head_image"]]; }else if ([tokenUrl isEqualToString:@"/repair/imageUploadToken"]) { url = [NSString stringWithFormat:@"%@",resp[@"key"]]; } if(success) { success(url); } }else{ if(failure) { failure(); } } }option:opt]; } failure:^{ failure(); }]; } //上传多张图片 + (void)uploadImages:(NSArray *)imageArray loadUrl:(NSString *)tokenUrl progress:(void (^)(CGFloat))progress success:(void (^)(NSArray *urlArray))success failure:(void (^)())failure { NSMutableArray *array = [[NSMutableArray alloc]init]; // __block NSMutableString *string = [NSMutableString string]; // __block CGFloat totalProgress =0.0f; // // __block CGFloat partProgress =1.0f/ [imageArray count]; __block NSUInteger currentIndex =0; QiniuUploadHelper *uploadHelper = [QiniuUploadHelper shareUploadHelper]; __weak typeof(uploadHelper) weakHelper = uploadHelper; uploadHelper.singleFailureBlock= ^() { failure(); return; }; uploadHelper.singleSuccessBlock= ^(NSString*url) { [array addObject:url]; // if (string.length > 0) { // string = [[string stringByAppendingString:@","] mutableCopy]; // string = [[string stringByAppendingString:url] mutableCopy]; // }else // string = [url mutableCopy]; // totalProgress += partProgress; // progress(totalProgress); currentIndex++; if(currentIndex == [imageArray count]) { // [array count] == [imageArray count] success([array mutableCopy]); // [array copy] return; }else{ [QiniuTool uploadImage:imageArray[currentIndex] loadUrl:tokenUrl progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock]; } }; [QiniuTool uploadImage:imageArray[0] loadUrl:tokenUrl progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock]; } /** 已时间给图片命名 @return pic name */ +(NSString *)getCurrentDateString { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSString *dateString = [formatter stringFromDate:[NSDate date]]; return dateString; } /* +(void)uploadVedio:(NSURL *)videoPathUrl success:(void (^)(NSMutableDictionary *result))success failure:(void (^)())failure { //上传视频 [QiniuTool getQiniuUploadToken:type_video result:^(NSDictionary *token) { if ([token count]) { NSData *data = http://www.mamicode.com/[NSData dataWithContentsOfURL:videoPathUrl];"upToken"]; NSString*fileName = [token objectForKey:@"key"]; QNUploadOption *opt = [[QNUploadOption alloc]initWithMime:nil progressHandler:nil params:nil checkCrc:NO cancellationSignal:nil]; QNUploadManager *upManager = [[QNUploadManager alloc] init]; [upManager putData:data key:fileName token:tokenStr complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) { if(info.statusCode == 200 && resp) { if(success) { NSMutableDictionary *resultDic = [NSMutableDictionary dictionary]; [resultDic setObject:[token objectForKey:@"url"] forKey:@"videoUrl"]; UIImage *videoImage = [LMUtil getImage:videoPathUrl]; // 上传视频缩略图 [self uploadImage:videoImage progress:nil success:^(NSString *url) { if (url) { [resultDic setObject:url forKey:@"videoImg"]; success(resultDic); } } failure:^{ failure(); }]; } }else{ if(failure) { failure(); } } } option:opt]; } } failure:^{ failure(); }]; } */ @end
辅助工具类类方法:
#import <Foundation/Foundation.h> @interface QiniuUploadHelper : NSObject @property(nonatomic, copy)void(^singleSuccessBlock)(NSString *); @property (nonatomic, copy)void(^singleFailureBlock)(); +(instancetype)shareUploadHelper; @end
实现:
#import "QiniuUploadHelper.h" @implementation QiniuUploadHelper static id _instance = nil; +(id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } +(instancetype)shareUploadHelper { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } - (id)copyWithZone:(NSZone*)zone { return _instance; } @end
图片、视频上传小记
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。