天天看點

AFNetworking斷點續傳、下載下傳圖檔

下載下傳圖檔:下載下傳圖檔後儲存在本地檔案中

//在lock和挂起狀态都會下載下傳  
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201110/05/082552rm0v11zlv1axd13l.png"]];  
//操作配置  
AFHTTPRequestOperation *requestOperator=[[AFHTTPRequestOperation alloc]initWithRequest:request];  
requestOperator.responseSerializer=[AFImageResponseSerializer serializer];  
[requestOperator setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){  
    //主線程中執行  
    self.pictureView.image=responseObject;  
    NSString *fileDestination=[NSHomeDirectory() stringByAppendingString:@"/Documents/1.png"];  
    <span style="color:#ff6666;">//若respondObject為NSData,則用下面的方法</span>  
    /*NSFileManager *fileManage=[NSFileManager defaultManager]; 
    if([fileManage createFileAtPath:fileDestination contents:imgData attributes:nil]){ 
        NSLog(@"%@",fileDestination); 
    }*/  
    if([UIImageJPEGRepresentation(responseObject, 1.0) writeToFile:fileDestination atomically:YES]){  
        //成功後運作  
        NSLog(@"%@",fileDestination);  
    }  
      
} failure:^(AFHTTPRequestOperation *operation, NSError *error){  
    NSLog(@"error:%@",error);  
}];  
//監聽下載下傳進度  
[requestOperator setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){  
    self.progressView.progress=totalBytesRead*1.0/totalBytesExpectedToRead;  
    //NSLog(@"%.3f",totalBytesRead*1.0/totalBytesExpectedToRead);  
}];  
//開始執行  
[requestOperator start];  
           

斷點續傳:暫停和開始下載下傳的按鈕我都是一個按鈕,是以定義了AFHTTPRequestOperation *resumeOperation和一個BOOL類型的resume判斷是否續傳。

因為在block中又調用了self,會造成記憶體洩露,是以定義了__weak的weakSelf解決記憶體洩露的問題

<span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;"><span style=</span><span class="string" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"color:#ff0000;"</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;">>__weak </span><span class="keyword" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">ViewController</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;"> *weakSelf=</span><span class="keyword" style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">self</span><span style="line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none; background-color: inherit;">;</span>  </span>
           
if(!self.resumeOperation){  
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201110/05/082552rm0v11zlv1axd13l.png"]];  
        self.resumeOperation=[[AFHTTPRequestOperation alloc]initWithRequest:request];  
        self.resumeOperation.responseSerializer=[AFImageResponseSerializer serializer];  
        [weakSelf.resumeOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){  
            //主線程中執行  
            self.pictureView.image=responseObject;  
            //儲存檔案  
            NSString *fileDestination=[NSHomeDirectory() stringByAppendingString:@"/Documents/1.png"];  
            if([UIImageJPEGRepresentation(responseObject, 1.0) writeToFile:fileDestination atomically:YES]){  
                NSLog(@"%@",fileDestination);  
            }  
            self.resumeOperation=nil;  
              
        } failure:^(AFHTTPRequestOperation *operation, NSError *error){  
            self.resumeOperation=nil;  
            NSLog(@"error:%@",error);  
        }];  
        //監聽下載下傳進度  
        [weakSelf.resumeOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){  
            self.progressView.progress=totalBytesRead*1.0/totalBytesExpectedToRead;  
            //NSLog(@"%.3f",totalBytesRead*1.0/totalBytesExpectedToRead);  
        }];  
        //開始執行  
        [self.resumeOperation start];  
    }else if(!self.resume){  
        [self.resumeOperation pause];  
        self.resume=YES;  
    }else{  
        //繼續  
        [self.resumeOperation resume];  
        self.resume=NO;  
    }