Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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
Ios 来自URL的UIImage(谷歌地图)_Ios_Google Maps_Uiimage - Fatal编程技术网

Ios 来自URL的UIImage(谷歌地图)

Ios 来自URL的UIImage(谷歌地图),ios,google-maps,uiimage,Ios,Google Maps,Uiimage,嗨,我正在尝试从maps.googleapis.com获取图像。以下是我的代码,因为某些原因,我的URL一直返回为零。我做错什么了吗 NString *imageSourceString = @"https://maps.googleapis.com/maps/api/staticmap? markers=%2212547%20Hardy%20St,Overland%20Park,Kansas%22&zoom=14&size=576x640&maptype=hybrid

嗨,我正在尝试从maps.googleapis.com获取图像。以下是我的代码,因为某些原因,我的URL一直返回为零。我做错什么了吗

NString *imageSourceString = @"https://maps.googleapis.com/maps/api/staticmap?  markers=%2212547%20Hardy%20St,Overland%20Park,Kansas%22&zoom=14&size=576x640&maptype=hybrid& sensor=false";

UIImage* finalBitmap = [UIImage imageNamed:imageSourceString];

if( nil == finalBitmap )
{
    // Next, try resolving the image using the string as the file name
    finalBitmap = [UIImage imageWithContentsOfFile:imageSourceString];

    if( nil == finalBitmap )
    {
        // Next, try resolving the string as a URL.

        NSURL* url = [NSURL URLWithString:imageSourceString];
        if( nil != url ) //url is nil at this point
        {
            NSData* data = [NSData dataWithContentsOfURL:url];
            if( nil != data )
            {
                finalBitmap = [UIImage imageWithData:data];
            }
            else
            {
                finalBitmap = nil;
            }
        }
        else
        {
            finalBitmap = nil;
        }

    }
}

这不可能是真正的代码,它甚至不会编译。粘贴真实的东西

首先,它是NSString,而不是NSString

其次,imageSourceString上的imageWithContentsOfFile将失败,因为您给了它一个URL,而不是文件名。你可以给它一个文件URL,但这里不是这样


第三,不要在真正的应用程序中使用带有contentsOfURL的数据-如果另一端的服务器速度太慢,它将冻结UI并可能使应用程序崩溃。使用异步网络。关于这个主题,有一系列优秀的WWDC视频。

不可能是真正的代码,它甚至不会编译。粘贴真实的东西

首先,它是NSString,而不是NSString

其次,imageSourceString上的imageWithContentsOfFile将失败,因为您给了它一个URL,而不是文件名。你可以给它一个文件URL,但这里不是这样

第三,不要在真正的应用程序中使用带有contentsOfURL的数据-如果另一端的服务器速度太慢,它将冻结UI并可能使应用程序崩溃。使用异步网络。关于这一主题,有一系列精彩的WWDC视频。

这非常有用:

NSString *stringURL = @"http://www.yoururl.com";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
[imageData release];
你的错误是

UIImage* finalBitmap = [UIImage imageNamed:imageSourceString];
无法从远程URL获取图像,因此

if( nil == finalBitmap )
将永远为零

还要检查服务器是否正确返回映像。

这非常有效:

NSString *stringURL = @"http://www.yoururl.com";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
[imageData release];
你的错误是

UIImage* finalBitmap = [UIImage imageNamed:imageSourceString];
无法从远程URL获取图像,因此

if( nil == finalBitmap )
将永远为零


还要检查服务器是否正确返回图像。

您的
imageSourceString
中是否包含这些空格?您的
imageSourceString
中是否包含这些空格?