我有两个视图控制器。在第一个 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
发布评论
评论(3)
您的问题是您正在发出同步请求以获取图像。这意味着您的主线程正在等待图像下载,然后继续执行其余流程。所以,“冻结”按钮只是表明主线程中正在发生某些事情,并且需要首先完成这个“事情”。
解决方案是进行异步调用来获取图像。我建议使用 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.
基本上在触摸按钮时..您正在连接到 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.. :)
根据您的问题,图像数据来自互联网,因此需要一些时间才能从互联网提取数据并显示在图像视图上,因此在这种情况下,我建议您为此使用线程这里是我根据您的问题使用的代码,这应该
在这里完美工作 img1 和 img2 是 UIImageVIew 类的对象
,并在 init 方法中使用 NSThread 类调用此函数
谢谢和问候
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
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
Thanks and Regards