取消AFHttpRequestOperation

时间:2015-02-28 10:23:29

标签: ios7 afhttprequestoperation

我正在使用AFHttpRequestOperation下载图像,我想在按钮点击时取消此操作。我该怎么做

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strImagePath]];
self.managerDownloadActualImage = [AFHTTPRequestOperationManager manager];
self.managerDownloadActualImage.responseSerializer = [AFHTTPResponseSerializer serializer];
self.operationImageDownload = [self.managerDownloadActualImage HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Download succeeded.");
    chatData.isDownloaded = [NSNumber numberWithBool:YES];
    chatData.isThumbDownloaded = [NSNumber numberWithBool:YES];
    NSData *responseData = (NSData *)responseObject;
    chatData.actual_path = [NSString stringWithFormat:@"%@",[self saveImageInDocumemntDirectoryWithImageData:responseData]];
    [self saveData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Download failed with error: %@", error);
}];

[self.operationImageDownload setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    NSLog(@"Bytes read: %lu", (unsigned long)bytesRead);
    NSLog(@"Total bytes read %lld", totalBytesRead);
    NSLog(@"Total progress: %Lf", (long double)totalBytesRead / totalBytesExpectedToRead);

    int currentPercentage = ((long double)totalBytesRead / totalBytesExpectedToRead) * 100;
    if (currentPercentage <= 100) {
        self.customProgressView.percent = currentPercentage;
        [self.customProgressView setNeedsDisplay];
    }
    else {
        [self.customProgressView removeFromSuperview];
    }
}];

NSLog(@"Starting download.");
[self.operationImageDownload start];

我用过

[self.managerDownloadActualImage.operationQueue cancelAllOperations];

但这不会停止下载过程。

0 个答案:

没有答案
相关问题