NSOperation 和 SetImage
我需要使用线程来从网络检索图像并将其分配到图像视图中。
我对 NSOperation 进行了子类化,并从我的视图控制器中调用它,如下所示:
NSOperation *operation = [[[GetImage alloc] init] autorelease];
[queue addOperation:operation];
我得到了很好的图像,但是如何分配 NSOperation 类中的图像?我将图像存储在名为 RetrievedImage 的 UIImage 中:
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
retrievedImage = [[UIImage alloc] initWithData:imageData];
[imageData release];
现在如何将其放入 ViewController 的 ImageView 中?
谢谢
I need to use a thread in order to retrieve an image from the web and assign it into an image view.
I have subclassed NSOperation and called it from my view controller like:
NSOperation *operation = [[[GetImage alloc] init] autorelease];
[queue addOperation:operation];
I get the image fine but how do I go about assigning that image that is in my NSOperation class? I have the image stored in a UIImage called RetrievedImage:
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
retrievedImage = [[UIImage alloc] initWithData:imageData];
[imageData release];
How do I now get that into the ImageView of my ViewController?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你使用委托。
你的 NSOperation:
你的委托:
重要的部分是检查你是否在主线程上运行的部分。
您无法从另一个线程访问界面元素。因此,如果您不在主线程上,则可以在主线程上启动相同的方法
you use delegation.
Your NSOperation:
your delegate:
the important part is the part where you check if you are running on the main thread.
You can't access interface elements from another thread. So if you aren't on main thread, you start the same method on the main thread
这里有几个选项:
我个人更喜欢 NSNotification 来处理这类事情 - 它实际上与 foursquare 的 完全加载 项目非常相似。
You have a few options here:
I personally prefer NSNotification for this sort of thing- it's actually pretty similar to foursquare's fully-loaded project.
尝试使用 HJCache,它是为这种事情而设计的:
http://www.markj.net/hjcache-iphone-image-cache/
Try using HJCache, its made for this kind of thing:
http://www.markj.net/hjcache-iphone-image-cache/
ImageView 有一个图像属性,尝试访问它。
The ImageView has an image property, try accessing this.