NSOperation 和 SetImage

发布于 2024-10-15 04:06:12 字数 540 浏览 7 评论 0原文

我需要使用线程来从网络检索图像并将其分配到图像视图中。

我对 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 技术交流群。

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

发布评论

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

评论(4

奶茶白久 2024-10-22 04:06:12

你使用委托。

你的 NSOperation:

- (void)main {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://sstatic.net/stackoverflow/img/apple-touch-icon.png"]];
    UIImage *image = [UIImage imageWithData:data];
    [delegate fetchedImage:image];
    [pool drain];
}

你的委托:

- (void)fetchedImage:(UIImage *)image {
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:_cmd withObject:image waitUntilDone:NO];
        return;
    }
    self.imageView.image = image;
}

重要的部分是检查你是否在主线程上运行的部分。
您无法从另一个线程访问界面元素。因此,如果您不在主线程上,则可以在主线程上启动相同的方法

you use delegation.

Your NSOperation:

- (void)main {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://sstatic.net/stackoverflow/img/apple-touch-icon.png"]];
    UIImage *image = [UIImage imageWithData:data];
    [delegate fetchedImage:image];
    [pool drain];
}

your delegate:

- (void)fetchedImage:(UIImage *)image {
    if (![NSThread isMainThread]) {
        [self performSelectorOnMainThread:_cmd withObject:image waitUntilDone:NO];
        return;
    }
    self.imageView.image = image;
}

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

雨后咖啡店 2024-10-22 04:06:12

这里有几个选项:

  1. 您可以将对 imageview 的引用传递给 GetImage 操作,并在该操作获取图像时分配给 imageview.image 。
  2. 您可以子类化 UIImageView 并让它监听通知,例如 GetImageFinishedNotification - 该操作在完成时发布此通知并附加图像,并且 imageview 接收它并显示图像。
  3. 您可以设置一个具有 UIImage 属性的模型对象。您的视图控制器保留该对象并在其图像属性上设置键值观察。该操作还在获取图像时保留它并设置图像属性。视图控制器会收到更改警报并设置正确的图像视图。

我个人更喜欢 NSNotification 来处理这类事情 - 它实际上与 foursquare 的 完全加载 项目非常相似。

You have a few options here:

  1. You could hand a reference to your imageview to the GetImage operation, and assign to imageview.image when the operation gets the image.
  2. You could subclass UIImageView and have it listen to a notification, say GetImageFinishedNotification- the operation posts this notification with the image attached when it finishes, and the imageview receives it and displays the image.
  3. You could set up a model object that has a UIImage property. Your viewcontroller retains on this object and sets up Key-Value observing on its image property. The operation also retains on it and sets the image property when it gets the image. The viewcontroller is alerted to the change and sets the right imageview.

I personally prefer NSNotification for this sort of thing- it's actually pretty similar to foursquare's fully-loaded project.

悸初 2024-10-22 04:06:12

尝试使用 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/

风尘浪孓 2024-10-22 04:06:12

ImageView 有一个图像属性,尝试访问它。

The ImageView has an image property, try accessing this.

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