简体   繁体   English

目标C-如何从另一个类(另一个UIViewController)更改图像

[英]Objective C - How to change image from another class (another UIViewController)

I'm using NSObject class to handle a few server methods. 我正在使用NSObject类来处理一些服务器方法。

One of the methods is to download images from DB (parse.com). 其中一种方法是从DB(parse.com)下载图像。

The problem is that the UIViewController who present the image is loaded faster than the download process (the image always nil ), and only 2 sec after that the image is in the device. 问题在于,显示图像的UIViewController加载速度比下载过程(图像始终为nil )要快,并且在将图像放入设备后仅2秒。

How can I update the UIImageView image (userProfile class) from the NSObject class (the one with the download method)? 如何从NSObject类(带有下载方法的类)更新UIImageView图像(userProfile类)?

  • I'm not using navigation bar - but the UIViewController with the image is in UITabBar 我没有使用导航栏-但是带有图像的UIViewControllerUITabBar

I know how to detect the end of the downloading process but I don't know how to reach to the UIImageView property from the other class. 我知道如何检测下载过程的结束,但是我不知道如何从另一个类访问UIImageView属性。

Use delegate for your requirement. 使用delegate来满足您的要求。 Check implement delegate or ios_delegates how it can be done. 检查实现委托ios_delegates如何完成。

Some understanding is given below : 以下是一些理解:

Say u have delegate method in NSObject class to handle a few server methods 说你在NSObject类中有委托方法来处理一些服务器方法

@protocol YouNSObjectClassDelegate <NSObject>
@required
  -(void)imageDownloaded:(UIImage*)image;
@end

Add

@property (nonatomic,strong) id<YouNSObjectClassDelegate> delegate;

While allocating object any any viewcontroller , don't forgot to intialize delegate 在分配对象的任何viewcontroller时,不要忘记初始化委托

yourNSObjectClassObject.delegate = self;

When image is downloaded in NSObject class 在NSObject类中下载图像时

if ([delegate respondsToSelector:@selector(imageDownloaded:)]) {
    [delegate imageDownloaded:image];
}

In UIViewController where u want to have image add these method 在你想要有图像的UIViewController中添加这些方法

-(void)imageDownloaded:(UIImage*)image
{
   //Set Image here
}

while you calling NSObject Class just pass your UIImageView with other parameters and when your image got downloaded just set new image to the UIImageView . 当您调用NSObject Class只需将UIImageView传递给其他参数,下载图像时,只需将新图像设置为UIImageView I guess you are using threading concept so update your UIImageView only on Main Thread . 我猜您正在使用线程概念,因此仅在Main Thread上更新UIImageView You can use either 您可以使用

dispatch_async(dispatch_get_main_queue(), ^(void){
        //Set your Image to UIImageView
    });

OR use 或使用

[self performSelectorOnMainThread:@selector(updateImage:) withObject:objects waitUntilDone:YES];

Or better you can write your own block or delegate method and pass downloaded image when image got downloaded. 或者更好的是,您可以编写自己的blockdelegate方法,并在下载图像时传递下载的图像。

In the tableView's cellForRowAtIndexPath method, rather than setting the image directly, use GCD async and a completion block. 在tableView的cellForRowAtIndexPath方法中,而不是直接设置图像,而是使用GCD异步和完成块。

In your download class header declare a new method with completion block: 在下载类标题中,声明一个带有完成块的新方法:

- (void)downloadImagesWithCompletion:(void (^)(void))completion;

And the implementation: 并执行:

- (void)downloadImagesWithCompletion:(void (^)(void))completion {
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, NO), ^(void){
        // Run your downloading here, and once your operation is complete...
        dispatch_async(dispatch_get_main_queue(), ^(void){
            completion();
        });
    });
}

In your view controller's handling method, call it like this: 在视图控制器的处理方法中,如下调用:

[downloader downloadImagesWithCompletion:^(void) {
{
    // your downloader is now complete, run your UI updates
}];

Note: this example doesn't include any return values in the completion block, but you can easily do that as well . 注意:此示例在完成块中不包含任何返回值,但是您也可以轻松地做到这一点

You'd better to think as UIViewController is a boss. 您最好考虑一下,因为UIViewController是老板。 That means UIViewController starts downloading a image, handle the completion, and set Tabbar image. 这意味着UIViewController开始下载图像,处理完成并设置Tabbar图像。 The class of download image process is just a worker. 下载映像过程的类别只是一个工作者。 The worker delegates a boss to important decisions. 工人将老板委派给重要的决定。

The class of download image process tries to set Tabbar image directly is not a good idea. 下载图像处理类试图直接设置Tabbar图像不是一个好主意。 It's a good idea to design the class has a delegate protocol or a block function to tell 'someone' to complete the download of image. 将类设计为具有委托协议或阻止功能以告知“某人”完成图像下载是一个好主意。

You'd better to use SDWebImage library. 您最好使用SDWebImage库。 The library is designed like I said. 图书馆的设计就像我说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM