将UIImageView从URL加载到UIScrollView,并使用UIActivityIndi​​catorView为每个UIImageView加载

时间:2011-08-03 12:45:55

标签: iphone uiscrollview uiimageview uiactivityindicatorview

我有以下代码可以正常工作UIScrollView。但是加载图片时只能在最后一个子视图中看到,UIActivityIndi​​catorView不能用于加载每张图片。

如何完成这项任务?

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

1 个答案:

答案 0 :(得分:0)

当你这样做时:

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

在for循环中,您为在for循环中创建的每个图像指定了相同的精确位置和尺寸。换句话说,您创建的每个下一个图像都将位于上一个图像的顶部,因此,您只能看到在for循环中创建的最终图像。

尝试将计数器“i”变量添加到CGRectMake()中的X和Y值,以抵消随后创建的每个图像。

因此,假设每个图像都是100像素×100像素,那么就像:

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

会使每个图像都位于前一个图像之后(因为每个图像的宽度为100像素,我们将其偏移100像素并将该值用作下一个图像的起点)。如果你在(i * 100)之后添加一个额外的值,那么它会变成(i * 100 + 50),那么你会得到一个距前一个图像50个像素的左边距。

如果你的图像尺寸不同,你需要先计算图像的宽度和高度,然后将它们计入你的CGRectMake()方法中。

希望有所帮助。

此外,您可以编写代码,而不是创建如此多的视图以添加到滚动视图中:

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