无法从网上下载图片

时间:2015-01-06 12:07:34

标签: objective-c networking uiimage

所以我有这种方法从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

是的,网址存在。我用另一种方法登录它: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

1 个答案:

答案 0 :(得分:0)

首先,您的代码缺少URL的协议 - NSURL可以处理不同的协议,并且不会默认为http。将http://前缀到您的网址,看看是否能获得一些数据。

其次,该网址实际上并不是图片的网址 - 我相信网址会返回整个HTML图片,这很可能不是您想要的(因为您正在尝试创建)带有数据的UIImage

相反,请尝试查看Imgur API(https://api.imgur.com) - 该页面上的链接甚至是示例Obj-C库(https://github.com/geoffmacd/ImgurSession