Hi Forum,
I am working on an app which retrieves a photo from the web using a given API. Now without multi-threading as we all know the UI will "hang" until the photoloads. I want to use GCD to let the UI flow while the picture loads. However I am having a hard time grasping how multi-threading works. Here is my code BEFORE multi-threading
so i decided to change it like this
a couple question i need help with
1. when the photo is being retrieved, lets say the size is 80MB right
we call the main thread so the data is being retrieved in a seperate thread, but right after i dispatch the main queue the next few lines all NEED the image I am getting to set the scroll view size and all. So if these are all running in the main thread how would they know how to set the scroll view and all that stuff?
[/LIST]
I am working on an app which retrieves a photo from the web using a given API. Now without multi-threading as we all know the UI will "hang" until the photoloads. I want to use GCD to let the UI flow while the picture loads. However I am having a hard time grasping how multi-threading works. Here is my code BEFORE multi-threading
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoImageScrollView.delegate = self;
self.photoImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:self.photoDictionary format:FlickrPhotoFormatLarge]]];
NSString *a = [self.photoDictionary objectForKey:@"title"];
self.title = a;
self.photoImageView.contentMode = UIViewContentModeTopLeft;
self.photoImageScrollView.contentSize = self.photoImageView.image.size;
self.photoImageView.frame = CGRectMake(0, 0, self.photoImageView.image.size.width, self.photoImageView.image.size.height);
self.photoImageScrollView.minimumZoomScale = .2;
self.photoImageScrollView.maximumZoomScale = 2.0;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
CGFloat defaultZoomScaleX = (self.view.frame.size.width-self.photoImageScrollView.frame.origin.x)/self.photoImageView.image.size.width;
CGFloat defaultZoomScaleY = (self.view.frame.size.height-self.photoImageScrollView.frame.origin.y)/self.photoImageView.image.size.height;
self.photoImageScrollView.zoomScale = MAX(defaultZoomScaleX, defaultZoomScaleY);
}
so i decided to change it like this
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoImageScrollView.delegate = self;
__block NSData *photoData = [[NSData alloc]init];
dispatch_queue_t downloadQueue = dispatch_queue_create("photo downloader", NULL);
dispatch_async(downloadQueue, ^
{
photoData = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:self.photoDictionary format:FlickrPhotoFormatLarge]];
dispatch_async(dispatch_get_main_queue(), ^
{
self.photoImageView.image = [UIImage imageWithData:photoData];
NSString *a = [self.photoDictionary objectForKey:@"title"];
self.title = a;
self.photoImageView.contentMode = UIViewContentModeTopLeft;
self.photoImageScrollView.contentSize = self.photoImageView.image.size;
self.photoImageView.frame = CGRectMake(0, 0, self.photoImageView.image.size.width, self.photoImageView.image.size.height);
self.photoImageScrollView.minimumZoomScale = .2;
self.photoImageScrollView.maximumZoomScale = 2.0;
[self viewWillAppear:YES];
});
});
dispatch_release(downloadQueue);
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
CGFloat defaultZoomScaleX = (self.view.frame.size.width-self.photoImageScrollView.frame.origin.x)/self.photoImageView.image.size.width;
CGFloat defaultZoomScaleY = (self.view.frame.size.height-self.photoImageScrollView.frame.origin.y)/self.photoImageView.image.size.height;
self.photoImageScrollView.zoomScale = MAX(defaultZoomScaleX, defaultZoomScaleY);
}
a couple question i need help with
1. when the photo is being retrieved, lets say the size is 80MB right
Code:
photoData = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:self.photoDictionary format:FlickrPhotoFormatLarge]];
[/LIST]