使用每个 UIImageView 的 UIActivityIndi​​catorView 将 UIImageView 从 URL 加载到 UIScrollView

发布于 2024-11-27 20:01:06 字数 3025 浏览 1 评论 0原文

我有以下代码,可以正常工作 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2024-12-04 20:01:06

当您这样做时:

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

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

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

因此,假设每个图像都是 100 像素 x 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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文