1

サーバーから画像を非同期でダウンロードしようとしています。画像をダウンロードして配置した後、画像を含む画像ビューを使用して、アプリを次のビュー コントローラーに移動させます。これが私のコードです:

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

問題は、次のView Controllerが約10〜15秒間何も表示されず、View Controllerに表示されるはずの画像とテキストが表示されることです。ここで私が間違っていることはありますか?

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 に答える