Upload notes for images and videos
Image uploading is often used in our development, such as the upload of an avatar, and publishing multiple images in the release space. Here, I use network resources for reference, it also relies on qiniu cloud storage to make a summary of Image Upload.
I first encapsulated a tool class for Image Upload, which is inherited from nsobject and named QiniuTool. When uploading multiple images, I also need to process the upload result of a single image. Therefore, here I have created an auxiliary tool class named QiniuUploadHelper;
Tool class method:
# Import <Foundation/Foundation. h> // upload a video and n images # import <QiniuSDK. h> # import <UIKit/UIKit. h> typedef enum {type_image, type_video} LoadType; @ interface QiniuTool: NSObject // get qiniu upload token + (void) getQiniuUploadToken :( LoadType) type loadUrl :( NSString *) tokenUrl result :( void (^) (NSDictionary * token) success failure :( void (^) () failure; /** upload the video @ param videoPathUrl the local URL of the video * // + (void) uploadVedio :( NSURL *) videoPathUrl success :( void (^) (NSMutableDictionary * result )) success failure :( void (^) () failure; /** upload a single image @ param progress upload progress @ param success callback @ param failure callback */+ (void) uploadImage :( UIImage *) image loadUrl :( NSString *) tokenUrl progress :( QNUpProgressHandler) progress success :( void (^) (NSString * url) success failure :( void (^) () failure; // upload multiple images by queue + (void) uploadImages :( NSArray *) imageArray loadUrl :( NSString *) tokenUrl progress :( void (^) (CGFloat )) progress success :( void (^) (NSArray * urlArray) success failure :( void (^) () failure; @ end
View Code
Implementation process:
# Import "QiniuTool. h "# import" QiniuUploadHelper. h "# import" EBirdDefine. h "@ implementation QiniuTool // get token of qiniu // here networkrequest is my most encapsulated network request tool class + (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 resp OnseObject, NSString * errorMsg) {if (errorCode = 0 & responseObject) {success (responseObject) ;}} failure: ^ (NSError * error) {failure () ;}] ;}// upload a single image + (void) uploadImage :( UIImage *) image loadUrl :( NSString *) tokenUrl progress :( QNUpProgressHandler) progress success :( void (^) (NSString *) success failure :( void (^) () failure {[QiniuTool getQiniuUploadToken: type_image loadUrl: tokenUrl result: ^ (NSDic Tionary * token) {// the key value in the block here should be retrieved based on background settings. The correct result NSString * tokenStr = [token objectForKey: @ "token"]; NSData * data = 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 failed: config]; [uploadManager putData: data key: fileName token: tokenStr complete: ^ (QNResponseInfo * info, NSString * key, NSDictionary * resp) {// The result here should be retrieved based on the background settings. if (info. statusCode = 200 & [resp count]) {// QiNiuBaseUrl NSString * url; if ([tokenUrl isEqualToString: @ "/customers/headImag EUploadToken "]) {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 () ;}];} // upload multiple images + (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 = [QiniuUplo AdHelper extends uploadHelper]; _ 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 mut AbleCopy]; // 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];}/** name the image @ 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 {// upload the video [QiniuTool failed: type_video result: ^ (NSDictionary * token) {if ([token count]) {NSData * data = [NSData dataWithContentsOfURL: videoPathUrl]; if (! Data) {if (failure) {failure ();} return;} NSString * tokenStr = [token objectForKey: @ "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]; // upload a video thumbnail [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
View Code
Auxiliary tool class methods:
#import <Foundation/Foundation.h>@interface QiniuUploadHelper : NSObject@property(nonatomic, copy)void(^singleSuccessBlock)(NSString *);@property (nonatomic, copy)void(^singleFailureBlock)();+(instancetype)shareUploadHelper;@end
View Code
Implementation:
#import "QiniuUploadHelper.h"@implementation QiniuUploadHelperstatic 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
View Code