上海网站建设市场分析,移动网站 做优化,山东省监理建设协会网站,wordpress 时间轴iOS开发-NSOperationQueue实现上传图片队列
在开发中#xff0c;遇到发帖需要上传图片#xff0c;需要上传队列#xff0c;这时候用到了NSOperationQueue
一、NSOperation与NSOperationQueue
什么NSOperation
NSOperation为控制任务状态、优先级、依赖关系以及任务管理提…iOS开发-NSOperationQueue实现上传图片队列
在开发中遇到发帖需要上传图片需要上传队列这时候用到了NSOperationQueue
一、NSOperation与NSOperationQueue
什么NSOperation
NSOperation为控制任务状态、优先级、依赖关系以及任务管理提供了一种线程安全的结构。可以通过调用start方法来手动启动一个任务或者把它加入到NSOperationQueue中当它到达队列头部时自动启动。 NSOperation的三个执行状态 isReady、 isExecuting、 isFinished
这三个是NSOperation生命周期中的三个互斥的状态。
二、通过NSOperationQueue添加NSOperation
NSOperationQueue
self.operationQueue [[NSOperationQueue alloc] init];
[self.operationQueue setMaxConcurrentOperationCount:1];在NSOperationQueue添加一系列的NSOperationaddDependency会使当前的NSOperation依赖上一个NSOperation完成后执行相应的block操作。
NSOperation *operation [[NSOperation alloc] init];[self.operationQueue addOperation:operation];for (NSInteger index 0; index self.files.count; index) {NSBlockOperation *theOperation [NSBlockOperation blockOperationWithBlock:^{[self uploadFileOperation:index];}];[theOperation addDependency:operation];[self.operationQueue addOperation:theOperation];operation theOperation;}三、实现上传功能
针对每一个上传的文件定义对应的文件model
SDUpFileModel.h
#import Foundation/Foundation.htypedef NS_ENUM(NSInteger, SDUpFileType) {SDUpFileTypeImage 0, //上传的图片类型SDUpFileTypeVoice, //上传的音频类型SDUpFileTypeVideo //上传的视频类型
};/**上传的资源类型modelimagevoicevideo*/
interface SDUpFileModel : NSObject/**上传的文件类型*/
property (nonatomic, assign) SDUpFileType fileType;/**上传到七牛的key*/
property (nonatomic, strong) NSString *key;/**上传所需要的token七牛的token*/
property (strong, nonatomic) NSString *token;/**上传的filePath文件本地路径*/
property (nonatomic, strong) NSString *filePath;/**上传所需要的参数视频参数图片参数语音参数等根据fileType参数不一样*/
property (nonatomic, strong) NSDictionary *params;endSDUpFileModel.m
#import SDUpFileModel.himplementation SDUpFileModelend上传的功能使用AFNetworking上传功能
如
NSURL *url [NSURL URLWithString:kQiniuUpHost];AFHTTPSessionManager *operationManager [[AFHTTPSessionManager alloc] initWithBaseURL:url];NSMutableDictionary *parameters [NSMutableDictionary dictionaryWithObjectsAndKeys:theFile.token,token,kQiniuUserAgent,User-Agent,nil];if (theFile.key){[parameters setObject:theFile.key forKey:key];[parameters setObject:url forKey:theFile.key];}if (theFile.params){for (NSString *key in theFile.params){[parameters setObject:[theFile.params objectForKey:key] forKey:key];}}NSMutableURLRequest *request [operationManager.requestSerializer multipartFormRequestWithMethod:POST URLString:kQiniuUpHost parameters:parameters constructingBodyWithBlock:^(idAFMultipartFormData formData) {if (theFile.fileType SDUpFileTypeImage) {//上传图片文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:image/jpeg error:nil];} else if (theFile.fileType SDUpFileTypeVoice) {//语音文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:audio/amr error:nil];} else if (theFile.fileType SDUpFileTypeVideo) {//mp4视频文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:video/mpeg4 error:nil];}} error:nil];__block NSURLSessionUploadTask *task nil;task [operationManager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {float percent uploadProgress.completedUnitCount * 1.0 / uploadProgress.totalUnitCount;if (self.upProgressHandler) {self.upProgressHandler(theFile, percent);}} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {NSHTTPURLResponse *taskresponse (NSHTTPURLResponse *)task.response;if(error nil) {NSInteger statusCode taskresponse.statusCode;if (statusCode 200){if (index self.files.count -1) {if (self.upAllComplectionHandler) {self.upAllComplectionHandler(self);}}} else{if (self.upFilesFailedHandler) {self.upFilesFailedHandler(self);}[self cancelAllUploadTask];}}else {NSString *errorStr [responseObject objectForKey:error];if ([errorStr isEqualToString:expired token]) {if (self.upFilesTokenErrorHandler) {self.upFilesTokenErrorHandler(self);}} else {if (self.upFilesFailedHandler) {self.upFilesFailedHandler(self);}}[self cancelAllUploadTask];}}];[task resume];上传工具的完整代码如下
SDUploaderTool.h
#import Foundation/Foundation.h
#import SDUpFileModel.hclass SDUploaderTool;/**上传单个文件进度的block*/
typedef void (^UploadProgressHandler)(SDUpFileModel *fileModel, float percent);/**上传所有文件成功的block*/
typedef void (^UploadAllCompletionHandler)(SDUploaderTool *uploader);/**上传文件失败的block上传文件的token异常重新上传重试3次*/
typedef void (^UploadFilesTokenErrorHandler)(SDUploaderTool *uploader);/**上传文件失败的block*/
typedef void (^UploadFilesFailedHandler)(SDUploaderTool *uploader);interface SDUploaderTool : NSObject/**上传单个文件进度的block*/
property (nonatomic, copy) UploadProgressHandler upProgressHandler;/**上传所有文件成功的block*/
property (nonatomic, copy) UploadAllCompletionHandler upAllComplectionHandler;/**上传文件失败的block上传文件的token异常重新上传重试3次*/
property (nonatomic, copy) UploadFilesTokenErrorHandler upFilesTokenErrorHandler;/**上传文件失败的block*/
property (nonatomic, copy) UploadFilesFailedHandler upFilesFailedHandler;/**上传的资源列表其中存储的是SDUpFileModel的对象*/
property (nonatomic, strong) NSMutableArray *files;/**添加上传文件param file SDUpFileModel*/
- (void)addFile:(SDUpFileModel *)file;/**添加多个上传文件param theFiles 文件列表*/
- (void)addFiles:(NSArray *)theFiles;/**开始上传启动上传*/
- (void)startUpload;/**取消所有的上传请求*/
- (void)cancelAllUploadTask;endSDUploaderTool.m
#import SDUploaderTool.h
#import AFNetworking.h#define kQiniuUpHost http://up.qiniu.com
#define kQiniuUndefinedKey ?
#define kQiniuUserAgent qiniu-ios-sdkinterface SDUploaderTool ()property (nonatomic, strong) NSOperationQueue *operationQueue;endimplementation SDUploaderTool- (instancetype)init
{self [super init];if (self) {self.files [[NSMutableArray alloc] init];self.operationQueue [[NSOperationQueue alloc] init];[self.operationQueue setMaxConcurrentOperationCount:1];}return self;
}/**添加上传文件param file SDUpFileModel*/
- (void)addFile:(SDUpFileModel *)file {[self.files addObject:file];
}/**添加多个上传文件param theFiles 文件列表*/
- (void)addFiles:(NSArray *)theFiles {[self.files addObjectsFromArray:theFiles];
}/**开始上传启动上传*/
- (void)startUpload {if (!(self.files self.files.count 0)) {return;}NSOperation *operation [[NSOperation alloc] init];[self.operationQueue addOperation:operation];for (NSInteger index 0; index self.files.count; index) {NSBlockOperation *theOperation [NSBlockOperation blockOperationWithBlock:^{[self uploadFileOperation:index];}];[theOperation addDependency:operation];[self.operationQueue addOperation:theOperation];operation theOperation;}
}/**取消所有的上传请求*/
- (void)cancelAllUploadTask {[self.operationQueue cancelAllOperations];
}/**执行上传使用AFNetWorkingparam index index*/
- (void)uploadFileOperation:(NSInteger)index {SDUpFileModel *theFile self.files[index];NSURL *url [NSURL URLWithString:kQiniuUpHost];AFHTTPSessionManager *operationManager [[AFHTTPSessionManager alloc] initWithBaseURL:url];NSMutableDictionary *parameters [NSMutableDictionary dictionaryWithObjectsAndKeys:theFile.token,token,kQiniuUserAgent,User-Agent,nil];if (theFile.key){[parameters setObject:theFile.key forKey:key];[parameters setObject:url forKey:theFile.key];}if (theFile.params){for (NSString *key in theFile.params){[parameters setObject:[theFile.params objectForKey:key] forKey:key];}}NSMutableURLRequest *request [operationManager.requestSerializer multipartFormRequestWithMethod:POST URLString:kQiniuUpHost parameters:parameters constructingBodyWithBlock:^(idAFMultipartFormData formData) {if (theFile.fileType SDUpFileTypeImage) {//上传图片文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:image/jpeg error:nil];} else if (theFile.fileType SDUpFileTypeVoice) {//语音文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:audio/amr error:nil];} else if (theFile.fileType SDUpFileTypeVideo) {//mp4视频文件[formData appendPartWithFileURL:[NSURL fileURLWithPath:theFile.filePath] name:file fileName:file mimeType:video/mpeg4 error:nil];}} error:nil];__block NSURLSessionUploadTask *task nil;task [operationManager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {float percent uploadProgress.completedUnitCount * 1.0 / uploadProgress.totalUnitCount;if (self.upProgressHandler) {self.upProgressHandler(theFile, percent);}} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {NSHTTPURLResponse *taskresponse (NSHTTPURLResponse *)task.response;if(error nil) {NSInteger statusCode taskresponse.statusCode;if (statusCode 200){if (index self.files.count -1) {if (self.upAllComplectionHandler) {self.upAllComplectionHandler(self);}}} else{if (self.upFilesFailedHandler) {self.upFilesFailedHandler(self);}[self cancelAllUploadTask];}}else {NSString *errorStr [responseObject objectForKey:error];if ([errorStr isEqualToString:expired token]) {if (self.upFilesTokenErrorHandler) {self.upFilesTokenErrorHandler(self);}} else {if (self.upFilesFailedHandler) {self.upFilesFailedHandler(self);}}[self cancelAllUploadTask];}}];[task resume];
}/**释放*/
- (void)dealloc {self.files nil;[self.operationQueue cancelAllOperations];self.operationQueue nil;
}end四、小结
iOS开发-NSOperationQueue实现上传图片队列。使用NSOperationQueueNSOperation通过AFnetworking上传功能实现。
学习记录每天不停进步。