开发者

Loading UIImageView from URL to UIScrollView with UIActivityIndicatorView for each UIImageView

I have the following code which works fine UIScrollView. But when loading the picture can be seen only at the last subview and UIActivityIndicatorView not working for loading each picture.

How to be done for such a task?

myProject2ViewController.h

#import <UIKit/UIKit.h>
#import "PageScrollView.h"

@interface myProject2ViewController : UIViewController
{
    NSMutableArray *pages;
    PageScrollView *scrollView;
    UIView *myOneSubview;
    UIImageView *imageView;
    UIActivityIndicatorView *myIndicator;
}

@property (nonatomic, retain) PageScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIActivityIndicatorView *myIndicator;

@end

myProject2ViewController.m

#import "myProject2ViewController.h"

@implementation myProject2ViewController

@synthesize scrollView;
@synthesize imageView;
@synthesize myIndicator;

- (void)dealloc
{
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *imageUrls = [NSArray arrayWithObjects:
                          @"link_1",
                          @"link_2",
                          @"link_3",
                          nil];

    pages = [[NSMutableArray alloc] init];

    for (int i = 0; i < [imageUrls count]; i++) {

        CGRect frameOne = CGRectMake(0.0f, 50.0f, 320.0f, 416.0f);
        myOneSubview = [[UIView alloc] initWithFrame:frameOne];
        myOneSubview.backgroundColor = [UIColor blackColor];

        NSString *imageUrl = [imageUrls objectAtIndex:i];

        myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        myIndicator.center = CGPointMake(160.0f, 130.0f);
        myIndicator.hidesWhenStopped = YES;
        myIndicator.backgroundColor = [UIColor clearColor];

        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 50.0f, 320.0f, 416.0f)];

        [NSThread detachNewThreadSelector:@selector(loadImages:) toTarget:self withObject:imageUrl];

        [myOneSubview addSubview:imageView];

        [pages addObject:myOneSubview];
    }

    scrollView = [[PageScrollView alloc] initWithFrame:self.view.frame];
    scrollView.pages = pages;
    scrollView.delegate = self;
    self.view = scrollView;
}

- (void)loadImages:(NSString *)string
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [myIndicator startAnimating];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:string]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
    [imageView setImage:image];

    [image release];
    [imageData release];

    [self performSelectorOnMainThread:@selector(loadImageComplete) withObject:self waitUntilDone:YES];

    [pool 开发者_开发技巧drain];
}

-(void)loadImageComplete
{
    NSLog(@"Load image complete!");
    [myIndicator stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

@end


When you do:

CGRectMake(0.0f, 50.0f, 320.0f, 416.0f);

Inside your for loop, you're specifying those same exact position and dimension for EVERY image you create in the for loop. In other words, every next image you create will sit ontop of the previous image, as a result, you will only see the final image you created in the for loop.

Try adding your counter "i" variable to the X and Y value in CGRectMake() to offset every image that gets subsequently created.

So let say each image was 100 pixel by 100 pixel, then something like:

CGRectMake(0.0f + (i * 100.0f), 50.0f, 320.0f, 416.0f);

Would make each image sit right after the previous one (since each image is 100 pixel wide, we offset it by 100 pixel and use that value as the starting point of the next image). If you add an extra value after (i * 100) so it becomes (i * 100 + 50) then that would give you a left padding of 50 pixels from the previous image.

If your images are different dimensions you'll need to calculate the width and height of the image before hand and factor those into your CGRectMake() method.

Hope that helps.

In addition, instead of creating so many views to add to your scroll view, you could write the code like so:

scrollView = [[UIScrollview alloc] init];

. . .

for(...)
{
    UIImageView *currentImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f + (i * 100.0f), 50.0f, 320.0f, 416.0f);
    [currentImage setImage:[UIImage imageNamed:[imageUrls objectAtIndex:i]]];

    . . .

    [scrollView addSubview: currentImage];
    [currentImage release];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜