开发者

UIButton freezes

I have two view controllers.In the first UIViewController i have a button which when clicked wi开发者_如何转开发ll take you to the the second view controller where two images are displayed.The images are from a URL,so i use NSData and initialize a UIImage,and assign it to the UIImageView.

Now the problem is when i click the UIButton in the first view controller the button remains in the pressed state for few seconds and then it moves to the second view controller to display the images.

    UIImage *Image1=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL_STRING]]];
    imageView1.contentMode=UIViewContentModeScaleAspectFit;
    imageView1.image=Image1;
    UIImage *Image2=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL_STRING]]];
    imageView2.contentMode=UIViewContentModeScaleAspectFit;
    imageView2.image=agent_logo_image;

Kindly anybody suggest me a apt solution to this problem.

Thank You


Your problem is that you are making a synchronous request in order to fetch the images. That means that your main thread is waiting for the image to download, and then it proceeds with the rest of the flow. So, the "frozen" button is just an indication that something is happening in the main thread, and this "something" needs to be finished first. The solution is to make an asynchronous call to get the images. I would recommend to use ASIHTTPRequest for that kind of stuff, as it is relatively easy to use, and saves you from a lot of work. If you don't want to use a library, you need to have a look at "Loading Data Asynchronously" in the NSURLConnection Class Reference.


Basically on touching the button..You are connecting to the URL to fetch the image.

This procedure on Main thread hangs the UI for a couple of seconds.

i would suggest you to put the image grabbing part in :

self performSelectorInBackground:@selector() withObject: function.

and then after grabing ur image use :

self performSelectorOnMainThread:@selector() withObject: waitUntilDone: to switch back to ur main thread and display ur images.

hope it helps.. :)


As per your question the image data is coming from the internet right so it will take some time so that the data is pulled from the internet and gets displayed on the imageview so in this case what i will suggest you is to use a thread for this kind of things heres a code that i have used based upon your problem this should work perfect

-(void)getImages
{
    NSString *firstImageURL = @"http://fc00.deviantart.net/fs33/i/2008/293/8/6/Wood_Apple_Wallpaper_by_diegocadorin.jpg";
    NSString *secondImageURL = @"http://i1-mac.softpedia-static.com/screenshots/Byte-Apple-Wallpaper_3.png";
    NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc]init];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:firstImageURL]];
    NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:secondImageURL]];
    UIImage * img = [[UIImage alloc]initWithData:data];
    UIImage *_img = [[UIImage alloc]initWithData:data2];
    img1.image = img ;
    img2.image = _img;
    [img release];
    [_img release];
    [threadPool drain];

}

here img1 and img2 are the objects of the UIImageVIew class

and into the init method make a call to this function using the NSThread class

[NSThread detachNewThreadSelector:@selector(getImages) toTarget:self withObject:nil];

Thanks and Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜