我遇到了一些代码问题,看看你们是否可以帮我解决这个问题。 我对iPhone的目标C和编程相当新,所以任何反馈都会很棒。
我试图从JSON文件中解析flickr的图像网址。我成功检索了数据并创建了发送到UIImageView所需的URL字符串。但是我似乎有问题,它在屏幕上显示图像。屏幕上显示的唯一内容是我的UIImageView框,它是蓝色但没有来自flickr的图像。我试图找出问题,我认为它与我的photoURLS可变数组没有正确的初始化有关,或者是因为当我从数组中打印出一个元素时它确实打印出来然后在打印出它的null之后。
如果你们可以帮我弄清楚什么是错的,或者甚至建议一个更好的替代方案来做我想做的事情会很棒。
以下是我的ViewController.h文件中唯一的内容:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic) NSMutableArray *photoURLS;
@end
以下是我在ViewController.m文件中的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(id)init {
self = [super init];
if(self) {
[self loadFlickrPhotos];
return self;
}
return nil;
}
-(void)loadFlickrPhotos {
NSString* urlstring = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?format=json&sort=random&method=flickr.photos.search&tags=rocket&tag_mode=all&api_key=0e2b6aaf8a6901c264acb91f151a3350&nojsoncallback=1"];
NSURL* url = [[NSURL alloc]initWithString:urlstring];
self.photoURLS = [[NSMutableArray alloc]init];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray* photos = [[parsedObject valueForKey:@"photos"]objectForKey:@"photo"];
for(NSDictionary* photo in photos) {
NSString* photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_m.jpg", [photo objectForKey:@"farm"],[photo objectForKey:@"server"],[photo objectForKey:@"id"],[photo objectForKey:@"secret"]];
[self.photoURLS addObject:[NSURL URLWithString:photoURLString]];
}
NSLog(@"Photo #: %i", self.photoURLS.count);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated {
NSData *imageData = [NSData dataWithContentsOfURL:[self.photoURLS objectAtIndex:0]];
NSLog(@"Photo from url is: %@",[self.photoURLS objectAtIndex:35]);
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 250, 250)];
imageView.backgroundColor = [UIColor blueColor];
imageView.image = [UIImage imageWithData:imageData];
[self.view addSubview:imageView];
[super viewDidAppear:YES];
}
@end
在控制台中,当我尝试检查它是否正常工作时,它看起来像这样:
2014-03-06 13:03:09.395 FlickrRocket[5011:11303] Photo #: 100
2014-03-06 13:03:09.705 FlickrRocket[5011:11303] Photo from url is: http://farm4.static.flickr.com/3678/12943650293_faf143aca1_m.jpg
2014-03-06 13:03:09.707 FlickrRocket[5011:11303] Photo from url is: (null)
答案 0 :(得分:0)
不要在init中加载照片。 试试这个:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadFlickrPhotos];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSData *imageData = [NSData dataWithContentsOfURL:[self.photoURLS objectAtIndex:0]];
NSLog(@"Photo from url is: %@",[self.photoURLS objectAtIndex:35]);
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 250, 250)];
imageView.backgroundColor = [UIColor blueColor];
imageView.image = [UIImage imageWithData:imageData];
[self.view addSubview:imageView];
}
- (void)loadFlickrPhotos
{
...
}