UIButton 冻结

发布于 2024-11-08 02:21:34 字数 671 浏览 4 评论 0 原文

我有两个视图控制器。在第一个 UIViewController 中,我有一个按钮,单击该按钮会将您带到显示两个图像的第二个视图控制器。图像来自 URL,所以我使用 NSData 并初始化 UIImage,然后分配它到 UIImageView。

现在的问题是,当我单击第一个视图控制器中的 UIButton 时,按钮保持按下状态几秒钟,然后移动到第二个视图控制器以显示图像。

    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;

请有人建议我解决这个问题的合适方法。

谢谢

I have two view controllers.In the first UIViewController i have a button which when clicked will 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-11-15 02:21:34

您的问题是您正在发出同步请求以获取图像。这意味着您的主线程正在等待图像下载,然后继续执行其余流程。所以,“冻结”按钮只是表明主线程中正在发生某些事情,并且需要首先完成这个“事情”。
解决方案是进行异步调用来获取图像。我建议使用 ASIHTTPRequest 来处理这类事情,因为它相对容易使用,并且节省你从很多工作中解脱出来。如果您不想使用库,则需要查看“异步加载数据”。

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.

莫言歌 2024-11-15 02:21:34

基本上在触摸按钮时..您正在连接到 URL 来获取图像。

主线程上的此过程会使 UI 挂起几秒钟。

我建议您将图像抓取部分放入:

self PerformSelectorInBackground:@selector() withObject: 函数中。

然后在抓取图像后使用:

self PerformSelectorOnMainThread:@selector() withObject: waitUntilDone:
切换回主线程并显示图像。

希望它有帮助..:)

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.. :)

独闯女儿国 2024-11-15 02:21:34

根据您的问题,图像数据来自互联网,因此需要一些时间才能从互联网提取数据并显示在图像视图上,因此在这种情况下,我建议您为此使用线程这里是我根据您的问题使用的代码,这应该

-(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];

}

在这里完美工作 img1 和 img2 是 UIImageVIew 类的对象

,并在 init 方法中使用 NSThread 类调用此函数

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

谢谢和问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文