- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImageimageNamed:@"AddStack.png"] style:UIBarButtonSystemItemAdd target:selfaction:@selector(performMethod)];
self.indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(160, 200, 20,20)];
self.indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.indicator.hidesWhenStopped = YES;
[self.view addSubview:self.indicator];
[self.indicator release];
}
- (void)performMethod {
self.indicator.hidden = NO;
[self.indicator startAnimating];
//Open another new async operation in the global queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://www.apple.com"];
NSError * error;
NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];
if (data != nil) {
//Inform main thread to refresh UI.
dispatch_async(dispatch_get_main_queue(), ^{
[self.indicator stopAnimating];
self.indicator.hidden = YES;
NSLog(@"Result: %@", data);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.indicator stopAnimating];
self.indicator.hidden = YES;
NSLog(@"error when download:%@", error);
});
}
});
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *cellIdentity = @"ImageViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentity] autorelease];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 90, 90)];
[cell.contentView addSubview:imageView];
[imageView release];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imagesUrls objectAtIndex:indexPath.row]]]];
dispatch_async(dispatch_get_main_queue(), ^{
[imageView setImage:img];
});
});
return cell;
}