1

Web サーバーから写真アプリに直接進行中の画像を保存しようとしています。これは私が現在進歩なしで使用しているものです:

NSURLSessionDownloadTask *dl = [[NSURLSession sharedSession]
                                    downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
                                            if ( !error )
                                            {
                                                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
                                                completionBlock(YES,image);
                                            } else{
                                                completionBlock(NO,nil);
                                            }

                                        }
                                    ];  
    [dl resume];

を使用してダウンロードの進行状況を取得できることはわかっていますが、イメージのダウンロードNSURLSessionDownloadDelegateに使用して、ダウンロード中に進行状況を取得したいと考えています。AFNetworkingAFNetworkingには、次のようなイメージビューで画像を表示する方法があることを知っています

[postCell.iv_postImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[[Utilities sharedUtilities] basePath],[item.imagePath substringFromIndex:2]]]];

しかし、画像をダウンロードするだけの方法はありますか?

4

4 に答える 4

0

を使用できますAFHTTPRequestOperation。を に設定responseSerializerAFImageResponseSerializerます。進行状況を取得するには、単純に を使用しsetDownloadProgressBlockます。以下は例です。

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImage *result = (UIImage *)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

}];

[requestOperation start];
于 2016-03-03T09:33:59.813 に答える
0

このようにしてください。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/image.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request
                                                                 progress:^(NSProgress * _Nonnull downloadProgress){  /* update your progress view  */ }
                                                              destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
                                                                            {
                                                                              NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
                                                                              return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
                                                                            }
                                                        completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
                                                                            {
                                                                                NSLog(@"File downloaded to: %@", filePath);
                                                                            }];
[downloadTask resume];
于 2016-03-03T08:13:33.873 に答える