不能从网上下载图片
can't download image from web
所以我有这种方法可以从 imgur.com 下载图像,如下所示:
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
self.imageTest = [UIImage imageWithData: imageData];
}
然后用这个记录数据的大小:
NSLog(@"%lx", (unsigned long)imageData.length);
但是日志中的图像大小显示
0
以前我使用这种异步方法:
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImage *imageMain = [UIImage imageWithData:data];
self.imageTest = [[UIImage alloc] init];
self.imageTest = [self imageWithImage:imageMain forRowAtIndexPath:indexPath];
}];
self.imageTest = [UIImage imageWithData: imageData];
}
但日志也显示 0
是的 url 存在。我已经用另一种方法登录了它:i.imgur.com/EXtwoxT 你可以自己看看
我做错了什么?如何检查图像是否已正确下载?
编辑:
对于那些说它是 URL 的人,您可以在下面查看我的实际日志。您可以在浏览器中复制并粘贴 URL 以查看它是否存在(存在)
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSLog(@"i.imgur.com/%@.jpg", [self.IDArray objectAtIndex:indexPath.row]);
//Asynchronous handler
// NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// [NSURLConnection sendAsynchronousRequest:urlRequest
// queue:[NSOperationQueue currentQueue]
// completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// UIImage *imageMain = [UIImage imageWithData:data];
// self.imageTest = [[UIImage alloc] init];
// self.imageTest = [self imageWithImage:imageMain forRowAtIndexPath:indexPath];
// NSLog(@"%lx", (unsigned long)imageData.length);
// }];
//Synchronous handler
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
self.imageTest = [UIImage imageWithData: imageData];
NSLog(@"%lx", (unsigned long)imageData.length);
}
2015-01-06 07:44:26.564 Huckleberry[12733:361944] i.imgur.com/EXtwoxT.jpg
2015-01-06 07:44:26.600 Huckleberry[12733:361944] 0
首先,您的代码缺少 URL 的协议 -- NSURL 可以处理不同的协议,而不仅仅默认为 http
。在您的 URL 前加上 http://
,看看是否能为您提供一些数据。
其次,URL 实际上并不是图像的 URL -- 我相信 URL 会 return 整个 HTML imgur页面,这很可能不是您想要的(因为您正在尝试使用数据创建 UIImage
)。
相反,尝试查看 Imgur API (https://api.imgur.com) -- there's even a link on that page to an example Obj-C library (https://github.com/geoffmacd/ImgurSession)
所以我有这种方法可以从 imgur.com 下载图像,如下所示:
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
self.imageTest = [UIImage imageWithData: imageData];
}
然后用这个记录数据的大小:
NSLog(@"%lx", (unsigned long)imageData.length);
但是日志中的图像大小显示
0
以前我使用这种异步方法:
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImage *imageMain = [UIImage imageWithData:data];
self.imageTest = [[UIImage alloc] init];
self.imageTest = [self imageWithImage:imageMain forRowAtIndexPath:indexPath];
}];
self.imageTest = [UIImage imageWithData: imageData];
}
但日志也显示 0
是的 url 存在。我已经用另一种方法登录了它:i.imgur.com/EXtwoxT 你可以自己看看
我做错了什么?如何检查图像是否已正确下载?
编辑:
对于那些说它是 URL 的人,您可以在下面查看我的实际日志。您可以在浏览器中复制并粘贴 URL 以查看它是否存在(存在)
- (void)getImageForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"i.imgur.com/%@.jpg",
[self.IDArray objectAtIndex:indexPath.row]]];
NSLog(@"i.imgur.com/%@.jpg", [self.IDArray objectAtIndex:indexPath.row]);
//Asynchronous handler
// NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// [NSURLConnection sendAsynchronousRequest:urlRequest
// queue:[NSOperationQueue currentQueue]
// completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// UIImage *imageMain = [UIImage imageWithData:data];
// self.imageTest = [[UIImage alloc] init];
// self.imageTest = [self imageWithImage:imageMain forRowAtIndexPath:indexPath];
// NSLog(@"%lx", (unsigned long)imageData.length);
// }];
//Synchronous handler
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url];
self.imageTest = [UIImage imageWithData: imageData];
NSLog(@"%lx", (unsigned long)imageData.length);
}
2015-01-06 07:44:26.564 Huckleberry[12733:361944] i.imgur.com/EXtwoxT.jpg
2015-01-06 07:44:26.600 Huckleberry[12733:361944] 0
首先,您的代码缺少 URL 的协议 -- NSURL 可以处理不同的协议,而不仅仅默认为 http
。在您的 URL 前加上 http://
,看看是否能为您提供一些数据。
其次,URL 实际上并不是图像的 URL -- 我相信 URL 会 return 整个 HTML imgur页面,这很可能不是您想要的(因为您正在尝试使用数据创建 UIImage
)。
相反,尝试查看 Imgur API (https://api.imgur.com) -- there's even a link on that page to an example Obj-C library (https://github.com/geoffmacd/ImgurSession)