Photos, videos upload a little note

Source: Internet
Author: User
Tags uikit

We are commonly used in the development of image upload, such as Avatar upload and so on, there are similar publishing space to say when the release of many pictures and so on, I am here by reference to network resources, and rely on the seven cow cloud storage to do a picture upload summary.

I first encapsulated a picture Upload tool class, inherit from NSObject, named Qiniutool, and then upload the picture, also need a single image upload results processing, therefore, I here have created an auxiliary tool class, named Qiniuuploadhelper;

Tool class Methods:

#import<Foundation/Foundation.h>//upload video, n photos#import<QiniuSDK.h>#import<UIKit/UIKit.h>typedefenum{type_image, type_video}loadtype;@interfaceQiniutool:nsobject//get seven kn upload token+ (void) Getqiniuuploadtoken: (loadtype) type Loadurl: (NSString *) Tokenurl Result: (void(^) (Nsdictionary *token)) Success Failure: (void(^) ()) failure;/** Upload video @param local URL path to videopathurl video*///+ (void) Uploadvedio: (Nsurl *) Videopathurl success: (void (^) (nsmutabledictionary *result)) Success failure: (void (^ ) ()) failure;/** Upload a single picture @param image @param Progress upload progress @param success Successful callback @param failure failure callback*/+ (void) Uploadimage: (uiimage*) Image loadurl: (NSString *) Tokenurl progress: (Qnupprogresshandler) Progress success: (void(^) (Nsstring*url)) Success Failure: (void(^) ()) failure;//upload multiple images, upload 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"@implementationQiniutool//get a token of seven kn//the networkrequest here 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,IDResponseobject, NSString *errormsg) {        if(ErrorCode = =0&&responseobject)        {success (responseobject); }} failure:^ (Nserror *error)    {failure (); }];}//upload a single photo+ (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) {//Here the key values in the block should be based on the background settings to remove the correct results 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:progressparams: nil Checkcrc:no Cancellationsignal:nil]; Qnuploadmanager*uploadmanager =[Qnuploadmanager Sharedinstancewithconfiguration:config]; [UploadManager putdata:data key:filename token:tokenstr Complete:^ (qnresponseinfo*info,nsstring*key,nsdictionary*resp) {//The results here should be taken out according to background settingsif(Info.statuscode = = $&& [resp Count]) {//QiniubaseurlNSString *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 (); }];}//upload more than one image+ (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]; __weaktypeof(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.singleSu        Ccessblock Failure:weakHelper.singleFailureBlock];    }    }; [Qiniutool uploadimage:imagearray[0] Loadurl:tokenurl progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];}/** have time to name the picture @return pic name*/+ (NSString *) getcurrentdatestring {nsdateformatter*formatter =[[NSDateFormatter alloc] init]; [Formatter Setdateformat:@"YYYY-MM-DD HH:mm:ss"]; NSString*datestring =[Formatter stringfromdate:[nsdate Date]]; returndatestring;}/*+ (void) Uploadvedio: (Nsurl *) Videopathurl success: (void (^) (nsmutabledictionary *result)) Success failure: (void (^) ())  Failure {//upload video [Qiniutool getqiniuuploadtoken: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 = = && resp) {               if (success) {Nsmutabledictionary *resultdic = [Nsmutabledictionary dictionary];                                                [Resultdic setobject:[token objectforkey:@ "url"] forkey:@ "Videourl"];                        UIImage *videoimage = [LMUtil Getimage:videopathurl];                            Uploading video thumbnails [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 Tools 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

Realize:

#import "QiniuUploadHelper.h"@implementationQiniuuploadhelperStatic ID_instance =Nil;+(ID) Allocwithzone: (struct_nszone *) Zone {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[Super Allocwithzone:zone];    }); return_instance;}+(instancetype) shareuploadhelper {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[[Self alloc] init];    }); return_instance;}- (ID) Copywithzone: (nszone*) Zone {return_instance;}@end
View Code

Photos, videos upload a little note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.