Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 异步下载图像_Ios_Objective C_Request - Fatal编程技术网

Ios 异步下载图像

Ios 异步下载图像,ios,objective-c,request,Ios,Objective C,Request,我正在尝试异步下载服务器上的映像。下载并放置图像后,我希望应用程序转到下一个视图控制器,其中包含图像视图。这是我的密码: NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://...../.png"]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemo

我正在尝试异步下载服务器上的映像。下载并放置图像后,我希望应用程序转到下一个视图控制器,其中包含图像视图。这是我的密码:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://...../.png"]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [NSURLConnection sendAsynchronousRequest:request queue:queue
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                                        if ( !error ) {
                                              image = [[UIImage alloc] initWithData:data];

                                              if (image != NULL) {
                                                    [self performSegueWithIdentifier:@"midnight" sender:self];
                                                               }
                                                       }

                                       NSLog(@"%@", image);
                          }];
    }

问题是,下一个视图控制器将在大约10-15秒的时间内不显示任何内容,然后显示应该显示在视图控制器上的图像和文本。我有什么地方做错了吗?

这对我来说很好。我使用了您的代码,但将操作队列更改为mainQueue,并添加了将图像传递到ImageViewController的代码:

#import "ViewController.h"
#import "ImageViewController.h"

@interface ViewController ()
@property (strong,nonatomic) UIImage *image;
@end

@implementation ViewController

-(IBAction)downloadPic:(id)sender {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://atmire.com/labs17/bitstream/handle/123456789/7618/earth-map-huge.jpg?sequence=1"]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               if ( !error ) {
                                   _image = [[UIImage alloc] initWithData:data];

                                   if (_image != NULL) {
                                       NSLog(@"Image id: %@", _image);
                                       NSLog(@"Image size is: %@", NSStringFromCGSize(_image.size));
                                       [self performSegueWithIdentifier:@"midnight" sender:self];
                                   }
                               }else{
                                   NSLog(@"Error is: %@",error);
                               }
                           }];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ImageViewController *ivc = segue.destinationViewController;
    ivc.receivedImage = _image;
}
在ImageViewController中,我创建了一个属性receivedImage,并且只有以下代码:

@interface ImageViewController ()
@property (weak,nonatomic) IBOutlet UIImageView *iv;
@end

@implementation ImageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.iv.image = self.receivedImage;
}

这对我很管用。我使用了您的代码,但将操作队列更改为mainQueue,并添加了将图像传递到ImageViewController的代码:

#import "ViewController.h"
#import "ImageViewController.h"

@interface ViewController ()
@property (strong,nonatomic) UIImage *image;
@end

@implementation ViewController

-(IBAction)downloadPic:(id)sender {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://atmire.com/labs17/bitstream/handle/123456789/7618/earth-map-huge.jpg?sequence=1"]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               if ( !error ) {
                                   _image = [[UIImage alloc] initWithData:data];

                                   if (_image != NULL) {
                                       NSLog(@"Image id: %@", _image);
                                       NSLog(@"Image size is: %@", NSStringFromCGSize(_image.size));
                                       [self performSegueWithIdentifier:@"midnight" sender:self];
                                   }
                               }else{
                                   NSLog(@"Error is: %@",error);
                               }
                           }];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ImageViewController *ivc = segue.destinationViewController;
    ivc.receivedImage = _image;
}
在ImageViewController中,我创建了一个属性receivedImage,并且只有以下代码:

@interface ImageViewController ()
@property (weak,nonatomic) IBOutlet UIImageView *iv;
@end

@implementation ImageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.iv.image = self.receivedImage;
}

如何将使用此方法获得的映像传递给下一个视图控制器?顺便说一句,您的队列应该是[NSOperationQueue mainQueue],因为您正在完成块中执行与UI相关的操作(推送)。我很惊讶,你甚至会得到你发布的代码的帮助。@rdelmar谢谢你的反馈。更改了它,现在可以工作了。您如何将使用此方法获得的映像传递给下一个视图控制器?顺便说一句,您的队列应该是[NSOperationQueue mainQueue],因为您正在完成块中执行与UI相关的操作(推送)。我很惊讶,你甚至会得到你发布的代码的帮助。@rdelmar谢谢你的反馈。改了,现在可以用了。谢谢!实际上,我刚从@rdelmar中意识到,当我将它改为mainQueue时,它就工作了+1@Zack,在进行更改之前,您是如何推动工作的?在“准备阶段”中,我告诉当前视图控制器上的图像传递到下一个UIImage,然后在视图中加载目标控制器以将UIImage加载到UIImageView中。从技术上讲,它能够工作,因为我从请求中获取一个值,并将数据传输到目标视图控制器。希望这有点道理@扎克,这根本不能解释为什么要表演赛格。当我使用[[NSOperationQueue alloc]init]尝试您的代码时,我从未获得该序列,因为它没有在主线程上被调用。你是不是在做别的事情来让赛格走?谢谢!实际上,我刚从@rdelmar中意识到,当我将它改为mainQueue时,它就工作了+1@Zack,在进行更改之前,您是如何推动工作的?在“准备阶段”中,我告诉当前视图控制器上的图像传递到下一个UIImage,然后在视图中加载目标控制器以将UIImage加载到UIImageView中。从技术上讲,它能够工作,因为我从请求中获取一个值,并将数据传输到目标视图控制器。希望这有点道理@扎克,这根本不能解释为什么要表演赛格。当我使用[[NSOperationQueue alloc]init]尝试您的代码时,我从未获得该序列,因为它没有在主线程上被调用。你是不是在做别的事情来让赛格走?