Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 无法使用NSURLConnection sendSynchronousRequest从url获取图像_Image_Url_Proxy_Nsurlconnection - Fatal编程技术网

Image 无法使用NSURLConnection sendSynchronousRequest从url获取图像

Image 无法使用NSURLConnection sendSynchronousRequest从url获取图像,image,url,proxy,nsurlconnection,Image,Url,Proxy,Nsurlconnection,我有一个在浏览器中返回图像的特殊URL。我想把这个特殊的图像放到UIImage对象中。但是,在下面的代码示例中,返回的数据似乎不是图像 浏览器似乎知道图像,如何才能访问图像 是的,我需要通过代理服务器获取此图像以解决我们的问题 下面是示例代码 -(UIImage*)retrieveMap { NSString* proxifiedGoogleMapURL = @"http://proxify.com/p/011010A1000100/687474703a2f2f6d617

我有一个在浏览器中返回图像的特殊URL。我想把这个特殊的图像放到UIImage对象中。但是,在下面的代码示例中,返回的数据似乎不是图像

浏览器似乎知道图像,如何才能访问图像

是的,我需要通过代理服务器获取此图像以解决我们的问题

下面是示例代码

-(UIImage*)retrieveMap
{        
    NSString* proxifiedGoogleMapURL = @"http://proxify.com/p/011010A1000100/687474703a2f2f6d6170732e676f6f676c652e636f6d2f6d6170732f6170692f7374617469636d61703f63656e7465723d34312e3031343130312c2d3130342e393738333333267a6f6f6d3d362673697a653d35303678353036267363616c653d32266d6170747970653d726f61647326666f726d61743d706e672673656e736f723d66616c7365";  

    NSURLRequest * firstRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: proxifiedGoogleMapURL]];
    NSURLResponse * firstResponse = nil; 
    NSError * firstError = nil;
    NSData* firstData = [NSURLConnection sendSynchronousRequest:firstRequest returningResponse:&firstResponse error:&firstError];

    UIImage* dataAsImage = [UIImage imageWithData:firstData];


    if(firstError != nil || dataAsImage == nil)
    {
        NSLog(@"Failed in retrieving the map");
    }
    else 
    {
        NSLog(@"Succeeded in retrieving the map");
    }

    return dataAsImage;
}

我发现了一些问题:

  • 您应该测试
    firstError!=在尝试转换为UIImage之前,请执行nil
    。之后再测试会让它变得毫无用处

  • 我试图访问你发布的URL,但我收到一个错误,说它只对登录的用户可用。在浏览器中,它可能适合您,因为您可能已经登录,但iphone没有,您可以从proxify获得HTML

  • 要查看得到的响应,请在创建图像之前将其转换为NSString并显示到控制台:

  • NSLog(@“来自服务器的响应:%@,[[NSString alloc]initWithData:firstData编码:NSUTF8StringEncoding]