gpt4 book ai didi

ios - 不使用 AFImageRequestOperation 的 AFNetworking 图片下载

转载 作者:行者123 更新时间:2023-11-28 20:04:07 25 4
gpt4 key购买 nike

我想从 url 下载图片并在响应对象中获取 NSData。

这是我设置操作的方式:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"image/jpeg"];
manager.responseSerializer = responseSerializer;

AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Accept"];
[requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer = requestSerializer;

[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSData *data = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

响应对象始终为零,url 正确,在浏览器中打开时显示图像,操作说明显示内容长度和内容类型(图像/jpeg),但响应对象似乎没有接收到它.

谢谢

回答:需要使用operation.reponseData代替responseObject。

最佳答案

您需要告诉 AFNetworking 您需要图像数据并且应该将其反序列化。

manager.responseSerializer = [AFImageResponseSerializer serializer];

AFImageResponseSerializer Class Reference

By default, AFImageSerializer accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage:

image/tiff
image/jpeg
image/gif
image/png
image/ico
image/x-icon
image/bmp
image/x-bmp
image/x-xbitmap
image/x-win-bitmap

Answer: instead of responseObject, operation.reponseData need to be used.

如果您要使用适当的序列化,它将是 responseObject,转换为您可以立即使用的对象。 operation.reponseData 包含从服务器发送的原始数据。通常这不是您所需要的。

关于ios - 不使用 AFImageRequestOperation 的 AFNetworking 图片下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22700467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com