1

I am trying to download an image off of a server asynchronously. After the image is downloaded and placed, I then want the app to go to the next view controller with an image view that will contain the image. Here is my code:

 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);
                          }];
    }

The problem is that the next view controller will come up with nothing on it for about 10-15 seconds and then show the image and the text that is supposed to be displayed on the view controller. Is there something that I am doing wrong here?

4

1 回答 1

1

这对我来说很好。我使用了您的代码,但将操作队列更改为 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;
}
于 2013-11-08T02:37:56.613 回答