AFNetworking断点续传、下载图片

时间:2022-04-07 15:54:42

下载图片:下载图片后保存在本地文件中

//在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;  
    }