基本的にはJSONで画像を取得し、画面に表示します。私の現在のコードでは、while
ループの後に画像を表示することを除いて、正常に動作します。
私が欲しいのは、ループが繰り返されるたびに画像を表示することです。可能であれば、AppleのTrailers.appのようにフェードする画像も表示します。
これが私の現在のコードです:
- (void)getNowShowingList2
{
self.navigationItem.rightBarButtonItem = nil;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
activityIndicator.hidesWhenStopped = YES;
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self navigationItem].rightBarButtonItem = barButton;
[activityIndicator startAnimating];
NSString *str = HOMEVIEW_URL;
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *jsonStr = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
// Checks for internet connectivity at startup
if ([jsonStr isEqualToString:@""])
{
HideNetworkActivityIndicator();
[self performSelectorOnMainThread:@selector(networkConnectivityCheck)
withObject:nil
waitUntilDone:YES];
}
else
{
// Parse the string into JSON
NSDictionary *json = [jsonStr JSONValue];
// Get all object
NSArray *items = [json valueForKeyPath:@"QUERY.DATA"];
NSEnumerator *enumerator = [items objectEnumerator];
NSArray *item;
while (item = [enumerator nextObject])
{
[myMovieId addObject:[item objectAtIndex:0]];
[myMovieTitle addObject:[item objectAtIndex:1]];
NSString *genre = @"";
if (genre == NULL)
{
genre = @"";
}
else
{
genre = [item objectAtIndex:2];
}
[myGenre addObject:genre];
[myCast addObject:[item objectAtIndex:3]];
[myPrice addObject:[item objectAtIndex:4]];
[myRuntime addObject:[item objectAtIndex:5]];
[myImageUrl addObject:[item objectAtIndex:6]];
[myRating addObject:[item objectAtIndex:7]];
[myHits addObject:[item objectAtIndex:8]];
NSURL *url = [NSURL URLWithString:[item objectAtIndex:6]];
NSData *data = [NSData dataWithContentsOfURL:url];
if (data == nil) // checks if internet connection was lost while parsing & fetching
{
HideNetworkActivityIndicator();
[self performSelectorOnMainThread:@selector(lostPrompt)
withObject:self
waitUntilDone:YES];
break;
}
else
{
[myImage addObject:[UIImage imageWithData:data]];
[self getMovieDetails:[item objectAtIndex:0]];
}
NSLog(@"O");
[self displayButton2];
}
activityIndicator.hidden = YES;
self.navigationItem.rightBarButtonItem = self.refreshButton;
}
[self performSelectorOnMainThread:@selector(versionCheck)
withObject:nil
waitUntilDone:YES];
}
- (void)displayButton2
{
int offset_x = 19;
int offset_y = 24;
for (int i = 0; i < [myImage count] ; i++)
{
// compute x & y offset
if (i % 3 == 0)
{
offset_x = 19;
}
else if (i % 3 == 1)
{
offset_x = 113;
}
else
{
offset_x = 208;
}
// Display Movie Thumbnail
UIImage *imageView1 = [myImage objectAtIndex:i];
CGRect myImageRect1 = CGRectMake(offset_x, offset_y, MOVIE_THUMBNAIL_W, MOVIE_THUMBNAIL_H);
UIButton *button1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button1.frame = myImageRect1;
button1.backgroundColor = [UIColor clearColor];
[button1 setBackgroundImage:imageView1 forState:UIControlStateNormal];
button1.tag = i;
[button1 addTarget:self
action:@selector(imageButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button1];
// Quick Buy Image
UIImage *buy_image = QUICK_BUY_BUTTON;
CGRect myImageRect2 = CGRectMake(offset_x + (MOVIE_THUMBNAIL_W-QUICK_BUY_BUTTON_W-2), offset_y + 2, QUICK_BUY_BUTTON_W, QUICK_BUY_BUTTON_H);
UIButton *button2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button2.frame = myImageRect2;
button2.backgroundColor = [UIColor clearColor];
[button2 setBackgroundImage:buy_image forState:UIControlStateNormal];
button2.tag = i;
[button2 addTarget:self
action:@selector(quickBuyButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button2];
// increment offset
if (offset_x == 208)
{
offset_y += 150;
}
}
if ((offset_x == 19) || (offset_x == 113))
{
offset_y += 158;
}
// Adjust scroll view
[scrollview setContentSize:CGSizeMake(0, offset_y + 10)];
// adjust background
[background setFrame:CGRectMake(13, 13, 294, offset_y - 17)];
backgroundHeightAdjust = offset_y - 17;
}
長期的には、AFNetworkingを使用することも計画しています。これは、達成したい効果に役立つ可能性があるためです。