iPhone - NSURLConnection asynchronous download using URLs in NSArray

那年仲夏 提交于 2019-12-06 15:40:23

you never download imageData. you assign it the request object . thats why you get the warning too. a NSURLConnection object is not a NSData object: NSData *imgData = [[NSURLConnection alloc] initWithRequest:imageRequest delegate:self]; //"Incompatible pointer types initializing ..." warning here

I would today rewrite it using the startAsyncConnection method. sec

-- there you go, untested and written in text edit but it should get you started (I reused most of your code but cut it down a lot too)

#import "RootViewController.h"

@interface RootViewController ()
@property(assign) IBOutlet UIScrollView *mainScroll;
@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self getJSONAndImageData];
}

- (void)getJSONAndImageData
{
    NSURL *url = [NSURL URLWithString:@"http://My server goes here/json.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse*r, NSData*d, NSError*e) {
        [self parseJSONAndGetImages:d];
    }];
}

- (void)parseJSONAndGetImages:(NSData*)data
{
    NSMutableArray *urlArray = [[NSMutableArray alloc] init];
    //This is where all the JSON Parsing is being done.
    //Turn off the data indicator, because the download is complete.
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    NSArray *jsonArray = (NSArray*)[NSJSONSerialization JSONObjectWithData:data options:nil error:nil];   //"Incompatible pointer types initializing ..." warning here => likely not an array then
    assert([jsonArray isKindOfClass:[NSArray class]]);

    //could be made in one liner with KVC
    //get the URL strings out of the jsonArray
    for (int x = 0; x < jsonArray.count; x++)
    {
        NSString *urlString = [[jsonArray objectAtIndex:x] objectForKey:@"image_URL"];
        NSLog(@"String is %@ ", urlString);
        [urlArray addObject:urlString];
    }

    [self loadImageArray:urlArray handler:^(NSArray* imageArray) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        for (int x = 0; x < imageArray.count; x++)
        {
            CGRect frame;
            frame.origin.x = self.mainScroll.frame.size.width * x;
            frame.origin.y = 0;
            frame.size = self.mainScroll.frame.size;

            UIImageView *nextIV = [[UIImageView alloc] initWithFrame:frame];
            [nextIV setImage:imageArray[x]];

            [self.mainScroll addSubview:nextIV];
            //NSLog(@"Pass %d", x);
        }

        self.mainScroll.contentSize = CGSizeMake(self.mainScroll.frame.size.width * imageArray.count,1.0);
    }];
}

//for SIMPLICITY I do synchronous networking here!
- (void)loadImageArray:(NSArray *)urlArray handler:(void(^)())handler {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableArray *imageArray = [NSMutableArray array];
        for (int y = 0; y < urlArray.count; y++)
        {
            NSString *urlString = [urlArray objectAtIndex:y];
            NSLog(@"Array String is: %@ ", urlString);
            NSURL *arrayURL = [NSURL URLWithString:urlString];
            NSURLRequest *imageRequest = [NSURLRequest requestWithURL:arrayURL];
            NSData *imgData = [NSURLConnection sendSynchronousRequest:imageRequest returningResponse:nil error:nil];
            UIImage *image = [UIImage imageWithData:imgData];
            [imageArray addObject:image];
        }

        dispatch_async(dispatch_get_main_queue(),^ {
            handler(imageArray);
        });
    });
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!