AFNetwoking上传图片进度条的显示

2017-03-02  本文已影响0人  凡凡饭饭

图片上传过程中进度条的使用

别处的文章,非本人自写,学习使用

MBProgressHUD* _hud;

_hud= [[MBProgressHUDalloc]initWithView:self.view];

_hud.labelText=@"正在上传";

_hud.mode=MBProgressHUDModeAnnularDeterminate;

[self.viewaddSubview:_hud];

3.0之后  有些许变化 http://www.jianshu.com/p/047463a7ce9b

progress部分反映了进度情况

上传图片是将数据流压缩了让后上传 ,也可不压缩

AFHTTPSessionManager* manager= [AFHTTPSessionManagermanager];

manager.responseSerializer.acceptableContentTypes= [NSSetsetWithObject:@"text/html"];

manager.responseSerializer= [AFHTTPResponseSerializerserializer];

[_hudshow:YES];

[managerPOST:@"Url 字符串"parameters:DictionaryconstructingBodyWithBlock:^(id_NonnullformData) {

NSData* imgData =UIImageJPEGRepresentation(_imgV.image,0.1);//0.1是把图片压缩

[formDataappendPartWithFileData:imgDataname:@"PHPhoto"fileName:@"phPhoto.jpg"mimeType:@"image/jpg/png/jpeg"];

}progress:^(NSProgress*_NonnulluploadProgress) {//进度 NSProgress 配合KVC来使用 反应进度情况

[uploadProgressaddObserver:selfforKeyPath:@"fractionCompleted"options:NSKeyValueObservingOptionNewcontext:nil];

}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

[_hudhide:YES];

idroot = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:NSJSONReadingAllowFragmentserror:nil];

NSLog(@"%@",root);

[selfperformSelectorOnMainThread:@selector(backToMain:)withObject:rootwaitUntilDone:YES];

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

[_hudhide:YES];

[selfperformSelectorOnMainThread:@selector(backToMain)withObject:nilwaitUntilDone:YES];

}];

#pragma mark -

#pragma mark上传进度

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context

{

if([keyPathisEqualToString:@"fractionCompleted"] && [objectisKindOfClass:[NSProgressclass]]) {

NSProgress*progress = (NSProgress*)object;

_hud.progress= progress.fractionCompleted;

}

}

#pragma mark -

这样就可以反映出 实时的上传进度了。

上一篇下一篇

猜你喜欢

热点阅读