Parse image issue in Iphone - iphone

I have a array which have a one content image URL for key "Image". I want to show that image in my table view.
How to do it .Please give me suggestion.

You can search in google using "lazy loading image for tableview".
It is also available with apple example.

img_view = [[UIImageView alloc]init];
img_view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
img_view.frame = CGRectMake(0,0,130, 75);
activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
[img_view addSubview: activityIndicator];
activityIndicator.center = CGPointMake(50,30);
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(loadImage:)
object:[[xmlParser data1] objectAtIndex:indexPath.row]];
[queue addOperation:operation];
[operation release];
[cell.contentView addSubview:img_view];
-(void)loadImage:(Book *)book
{
[activityIndicator startAnimating];
NSString *imgurl=[book image_url];
NSURL *url=[[NSURL alloc]initWithString:imgurl];
NSData *img=[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:img];
img_view.image=image;
[activityIndicator stopAnimating];
}

Related

How to stop mutiple activity indicator animation in multiple webview?

Currently i am developing a sample where i am displaying 4 webview in ipad screen and trying to show 4 different URL in them.
All the web pages are showing in the webview perfectly.
Also i am displaying 4 activity indicator once the webpage starts loading.
But i can not figure out how to stop activity indicator in all the respective webpage one by one once they are loaded in to the screen.
-(void)createWebView
{
//******************** First *********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second *********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third ************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth ************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
[firstIndicator release];
[secondIndicator release];
[thirdIndicator release];
[fourthIndicator release];
}
overwrite below delegate method and check which webview is loading finished like below
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(webView== firstWebView)
{
[firstIndicator setHidden:YES];
}
else
{
so on...
}
}

Multiple loading of webpages in webview is slower

I am developing an sample iPad app where I am displaying some webapages in UIWebview , the code is working properly.But I think the webpages are taking too much time to load in to UIWebview.My code is as below:
-(void)createWebView
{
//******************** First Webview*********
firstWebView=[[UIWebView alloc]init];
firstWebView.frame=CGRectMake(10, 50, 350, 470);
firstWebView.delegate=self;
firstIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
firstIndicator.center = CGPointMake(160, 240);
firstIndicator.hidesWhenStopped = YES;
[self.firstWebView addSubview:firstIndicator];
[firstIndicator startAnimating];
firstWebView.backgroundColor=[UIColor grayColor];
NSURL *firstUrl = [NSURL URLWithString:#"http://www.techtree.com"];
NSURLRequest *firstRequestObj = [NSURLRequest requestWithURL:firstUrl];
[firstWebView loadRequest:firstRequestObj];
[self.view addSubview:firstWebView];
//******************* Second Webview*********
secondWebView=[[UIWebView alloc]init];
secondWebView.frame=CGRectMake(405, 50, 350, 470);
secondWebView.delegate=self;
secondIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
secondIndicator.center = CGPointMake(160, 240);
secondIndicator.hidesWhenStopped = YES;
[self.secondWebView addSubview:secondIndicator];
[secondIndicator startAnimating];
secondWebView.backgroundColor=[UIColor grayColor];
NSURL *secondUrl = [NSURL URLWithString:#"http://www.facebook.com"];
NSURLRequest *secondRequestObj = [NSURLRequest requestWithURL:secondUrl];
[secondWebView loadRequest:secondRequestObj];
[self.view addSubview:secondWebView];
//****************** Third Webview************
thirdWebView=[[UIWebView alloc] init];
thirdWebView.frame=CGRectMake(10, 528, 350, 470);
thirdWebView.delegate=self;
thirdIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
thirdIndicator.center = CGPointMake(160, 240);
thirdIndicator.hidesWhenStopped = YES;
[self.thirdWebView addSubview:thirdIndicator];
[thirdIndicator startAnimating];
thirdWebView.backgroundColor=[UIColor grayColor];
NSURL *thirdUrl = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *thirdRequestObj = [NSURLRequest requestWithURL:thirdUrl];
[thirdWebView loadRequest:thirdRequestObj];
[self.view addSubview:thirdWebView];
//***************** Fourth Webview************
fourthWebView=[[UIWebView alloc] init];
fourthWebView.frame=CGRectMake(405,528, 350, 470);
fourthWebView.delegate=self;
fourthIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
fourthIndicator.center = CGPointMake(160, 240);
fourthIndicator.hidesWhenStopped = YES;
[self.fourthWebView addSubview:fourthIndicator];
[fourthIndicator startAnimating];
fourthWebView.backgroundColor=[UIColor grayColor];
NSURL *fourthUrl = [NSURL URLWithString:#"http://stackoverflow.com"];
NSURLRequest *fourthRequestObj = [NSURLRequest requestWithURL:fourthUrl];
[fourthWebView loadRequest:fourthRequestObj];
[self.view addSubview:fourthWebView];
//******************** Memory Managemt **********
[firstWebView release];
[secondWebView release];
[thirdWebView release];
[fourthWebView release];
}
So how can I make it more faster, please guide me.
For this you have to use NSOpertaionQueue or GCD queue is another options

Error in Load image in NSThread?

I have a url which contain image address, i want to load that image via NSThread but i am facing problem. I am doing thing like this.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 55, 57)];
[NSThread detachNewThreadSelector:#selector(showImage) toTarget:self withObject:nil];
[self.view addSubview:imageView];
- (void) showImage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:temp.strUrl];
UIImage *chart = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
[url release];
imageView.image = chart;
[pool release];
}
Please help me on that.
the problem is here.
[url release];
you are not supposed to release the url object . as you haven't alocated it, may be that is what you are facing problem with.

UIButton image asynchronously

I have a bunch of UIButton in a UIScrollView and each UIButton takes an image from a URL. What is the easiest way so that the image will be loaded asynchronously?
For example, the usual way is:
[button setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
but this blocks the UI, I don't want it to
You can try this
[self performSelectorInBackground:#selector(loadImag) withObject:nil];
In loadImage function load the image from url and then assign it to the button.
I am not sure this will work for you...As I am a beginner in objective C development
Try with an NSInvocationOperation, make an synchronous request for each image button... Pass the button as parameter, something like this i mean...
Init the operation queue (maybe on init):
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
start the operation invocation for each button...
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:#selector(getImageFromURL:)
object:[NSDictionary dictionaryWithObjectsAndKeys:#"http://getMyImage.com/resource.jpg", #"url", button, #"button", nil]];
[queue addOperation:operation];
[operation release];
this can be your getImageFromURL: selector
- (void) getImageFromURL:(NSDictionary*)dict
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLFromString:[dict objectForKey:#"url"]];
UIButton *button = [dict objectForKey:#"button"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
UIImage *image = [[UIImage alloc] initWithData:data];
// Finally set the button image and release image ...
[pool drain];
}
Don't forget release queue on dealloc...
Hope this helps! :)
I guess it is alright, if you block while loading one single image? The problem is that you have many of them? If so, then I would do like this (no threads needed):
#define DELAY 0.1 // you may set it to 0 as well
...
[self performSelector:#selector(setupButton:)
withObject:[NSNumber numberWithInt:0]
afterDelay:DELAY];
...
-(void)setupButton:(NSNumber*)count
{
UIButton *button = [self buttonFromMyScrollViewWithCount:count.intValue];
[button setImage:...];
if (count.intValue < self.numberOfButtonsInMyScrollView)
[self performSelector:#selector(setupButton:)
withObject:[NSNumber numberWithInt:count.intValue + 1]
afterDelay:DELAY];
}

Slow speed while converting NSDATA to UIImage

I have some images in the array in the form of NSDATA.and i am showing them on page in page controller example 6 at a time.
But they are taking time to get convert in to UIImage that's why scrolling get slow dowwn is we have any other option?
i am using following code to convert them into NSDATA.
[UImage imageWithData:data];
From http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation :
In your main thread:
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(loadImage)
object:nil];
[queue addOperation:operation];
[operation release];
Other methods required:
- (void)loadImage {
NSData* imageData = //however you're getting your NSData
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
[imageData release];
[self performSelectorOnMainThread:#selector(displayImage:) withObject:image waitUntilDone:NO];
}
- (void)displayImage:(UIImage *)image {
[imageView setImage:image]; //UIImageView
}