gpt4 book ai didi

ios - 将图像从 Google Places Photo JSON 返回到 imageView

转载 作者:行者123 更新时间:2023-11-29 10:49:43 25 4
gpt4 key购买 nike

我希望从 Google places API 获取照片,并将它们存储到 URL 或图像中,以便在单独的 View Controller 中显示给用户。

似乎照片 json 为每个位置返回了这个:

    photos =  (
{
height = 466;
"html_attributions" = ();
"photo_reference" = "CnRnAAAAgBUy_mqt9WglYJvS2v8XBfw5ER1U8Wn7DWvfoWI4P78w8_ZAsLQeFSYescNYE1NVgkV50jJE7SYBxdZuOmGP4jWGyCLobCd2nyEDbeB9lg1JU7KYV0o57i-OQTROIwj9ZwzWG03aUOypMjA_7fXD8BIQqbC5J0daROt_LqztWmz-xhoU20meJb50VyAE-zqkp9Jzu1Fegfs";
width = 695;
}
);

根据 Google Places Photo 文档,它说:

https://developers.google.com/places/documentation/photos

无法找到能够从 json 中获取此内容并将其存储为字符串的示例,因为 url 不是图像或任何内容的直接链接。

编辑:出现错误

[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x13258510
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x13258510'

更新代码:

-(void)plotPositions:(NSArray *)data {

for (id<MKAnnotation> annotation in _mapView.annotations) {
if ([annotation isKindOfClass:[MapPoint class]]) {
[_mapView removeAnnotation:annotation];
}
}
// Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++) {

NSDictionary* place = [data objectAtIndex:i];

NSDictionary *geo = [place objectForKey:@"geometry"];

NSDictionary *loc = [geo objectForKey:@"location"];

// Getting Photo Reference Details - ADDED NOW RECIEVING ERROR?
NSDictionary *photoDict = [place objectForKey:@"photos"];
NSString *photoRef = [photoDict objectForKey:@"photo_reference"];

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/photo?photoreference=%@&key=%@&sensor=false&maxwidth=320", photoRef, kGOOGLE_API_KEY];

NSLog(@"TESTING: %@", url);


NSString *name=[place objectForKey:@"name"];
NSString *vicinity=[place objectForKey:@"vicinity"];


CLLocationCoordinate2D placeCoord;
placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity rating:rating coordinate:placeCoord];

[_mapView addAnnotation:placeObject];
}
}

最佳答案

JSON 中的 photo_reference 属性是一个参数,您可以将其用作发出照片请求的参数。

在您引用的 url ( https://developers.google.com/places/documentation/photos ) 中,请特别查看“放置照片请求”部分,以正确的方式实际构建将请求图像的 url。

根据您上面的代码,您没有处理返回的照片数组。假设您想要第一张照片:

NSDictionary *photoDict = [[place objectForKey:@"photos"] objectAtIndex:0];
NSString *photoRef = [photoDict objectForKey:@"photo_reference"];

NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/photo?photoreference=%@&key=%@&sensor=false&maxwidth=320", photoRef, kGOOGLE_API_KEY];

关于ios - 将图像从 Google Places Photo JSON 返回到 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21004589/

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