圖片上傳過程中進度條的使用
別處的文章,非本人自寫,學習使用
MBProgressHUD* _hud;
_hud= [[MBProgressHUDalloc]initWithView:self.view];
_hud.labelText=@"正在上傳";
_hud.mode=MBProgressHUDModeAnnularDeterminate;
[self.viewaddSubview:_hud];
3.0之后 ?有些許變化?http://www.lxweimin.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 -
這樣就可以反映出 實時的上傳進度了。