我正在使用 Flickr API 和 ObjectiveFlickr 构建一个免费的乐器 iPhone 应用程序。兴趣列表中的随机照片显示在背景中,但在不知道其大小的情况下我无法将其居中。 (这样我就可以重置
UIWebView
框架)
我对此已经研究了一段时间,这是我第一次使用 Web 服务 API。 =)
由于直到收到兴趣源的响应后我才知道照片 ID,因此我如何在响应中调用
flickr.photo.getSizes
?这是我到目前为止所拥有的:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary{
int randomResponse = arc4random() % 49;
photoDict = [[inResponseDictionary valueForKeyPath:@"photos.photo"] objectAtIndex:randomResponse];
NSString *photoID = [photoDict valueForKeyPath:@"id"];
NSLog(@"%@",photoID);
NSURL *photoURL = [flickrContext photoSourceURLFromDictionary:photoDict size:OFFlickrMediumSize];
NSString *htmlSource = [NSString stringWithFormat:
@"<html>"
@"<head>"
@" <style>body { margin: 0; padding: 0; } </style>"
@"</head>"
@"<body>"
@"<img src=\"%@\" />"
@"</body>"
@"</html>"
, photoURL];
[webView loadHTMLString:htmlSource baseURL:nil];
}
我通过将返回的图像加载到 UIImageView 中实现了我想要的行为。这是代码,希望对某人有帮助:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary{
int randomResponse = arc4random() % 49;
photoDict = [[inResponseDictionary valueForKeyPath:@"photos.photo"] objectAtIndex:randomResponse];
NSString *photoID = [photoDict valueForKeyPath:@"id"];
NSLog(@"%@",photoID);
NSURL *photoURL = [flickrContext photoSourceURLFromDictionary:photoDict size:OFFlickrMediumSize];
NSData *receivedData = [[NSData dataWithContentsOfURL:
photoURL] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
NSLog(@"%i",image.size);
randomImage.image = image;
//[webView loadHTMLString:htmlSource baseURL:nil];
}