2011-08-03 61 views
0

我有以下代碼,它工作正常UIScrollView。但是當加載圖片只能在最後一個子視圖和UIActivityIndi​​catorView不能用於加載每張圖片時才能看到。從URL加載UIImageView到UIScrollView與UIActivityIndi​​catorView爲每個UIImageView

如何完成這樣的任務?

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 

回答

0

當你這樣做:

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

內,您的for循環,你指定的相同確切的寶您在for循環中創建的每張圖片的尺寸和尺寸。換句話說,您創建的每個下一張圖片都將放在上一張圖片的頂部,因此,您只會看到您在for循環中創建的最終圖片。

嘗試在CGRectMake()中將您的計數器「i」變量添加到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]; 
} 
+0

謝謝您的答覆。但ScrollView工作正常。這是由一個單獨的類定義的(#import「PageScrollView.h」 。)該類從一組「頁面」創建一個工作ScrollView。 – scazy0rg

相關問題