Amir Foghel
Amir Foghel

Reputation: 975

how to load few images from URL individually to scroll view ? objective-c

i have this scroll view, that I'm loading into him few images from URL. the problem is that the scroll view don't show any of them until that all loaded. i want to show every image the moment i finished loading her.

my code looks like this:

-(void)loadPhotosToLeftscroll{

    for (int count = 0 ; count < [leftPhotoArray count]; count++) {
        NSLog(@"nextPhotoHight: %f",nextLeftPhotoHight);

        NSMutableDictionary *photoDict;
        photoDict = [leftPhotoArray objectAtIndex:count];

        float photoHight = [[photoDict objectForKey:@"photos_height"]floatValue];
        float photoWide= [[photoDict objectForKey:@"photos_width"]floatValue];
        NSString *photoPath = [photoDict objectForKey:@"photos_path"];

        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:photoPath]];

        UIImage *image = [[UIImage alloc] initWithData:imageData];

        UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(10 , nextLeftPhotoHight, photoWide, photoHight )];

        [photoView setImage:image]; 
        [photoView.layer setMasksToBounds:YES];
        [photoView.layer setCornerRadius:6];

        nextLeftPhotoHight = photoView.frame.size.height + photoView.frame.origin.y + 10;

        [leftBlockScroll addSubview:photoView];
    }

}

Upvotes: 0

Views: 216

Answers (1)

TedCheng
TedCheng

Reputation: 655

You better use a asynchronous way to do that.

Relative topic is NSURLConnection, UIImageView.

I have done something similar before. 1. Create a new model inherit to UIView 2. This model will have a UIImageView, NSData 3. When u init the model, pass in a URL 4. Use NSURLConnection to send out AsynchroizedRequest 5. By using NSURLConnection delegate, you will finally get the Data of the image 6. Init a UIImage with these data 7. Init The UIImageView with this Image 8. Add this imageview to this model or directly pointing this model to the imageview

Feel free to ask for more detail :)

Upvotes: 1

Related Questions