0

我是 iOS 开发的新手,所以请多多包涵。我正在尝试创建一个基本的照片库,但遇到了问题。当我开始这个项目时,我只是将所有图像都包含在我的项目中。现在,在拥有更多图像(400+)之后,我开始从服务器加载它们。我使用以下代码行制作了一组图像:

[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.testsite.com/testPic.png"]]]

显然,让用户等待从服务器加载 400 多个图像的数组是不可接受的。

所以我的问题是,如果我在我的项目中包含一张图片,上面写着“正在加载”,我如何才能显示该图片,直到从服务器加载实际图片?

我正在使用表格视图和滚动视图制作一个基本的网格样式照片库。它会加载几行小(缩略图)图像,当您单击一个时,它会全屏显示。

如果有帮助,我正在使用 Xcode 4.3、ARC 和故事板!

对不起,如果这令人困惑!

-碎纸机2794

4

2 回答 2

1

一种方法是子类化UIImageView。当您分配它时,您放置默认图像(加载图像)或 a UIActivityIndicator,下载要显示的图像(在单独的线程中),并在下载图像时显示它。查看NSURLRequestNSURLConnection下载图像。

* 编辑 *

这是我开发的代码示例。您可以以此为起点来开发您自己的加载图像类。可以通过NSThread用于图像加载来改进此类。

// ImageLoader.h


#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

/**
 *  @brief  Class that loads an UIImage from a server
 *  @author Nicolas
 */
@interface ImageLoader : UIView 
{
    @private
    NSURLConnection         *connection;
    NSMutableData           *data;
    NSString                *path;
    UIActivityIndicatorView *loading;
    UIImageView             *imageView;
}

@property (nonatomic, retain) UIActivityIndicatorView   *loading;
@property (nonatomic, retain) NSURLConnection           *connection;
@property (nonatomic, retain) NSMutableData             *data;
@property (nonatomic, retain) NSString                  *path;
@property (nonatomic, retain) UIImageView               *imageView;

/**
 *  Load an image from a server an display it
 *  @param URL URL to get the image
 *  @param chemin path to save the image
 *  @author Nicolas
 */
- (void)loadImageFromUrl:(NSString *)URL forPath:(NSString *)chemin;

@end



// ImageLoader.m


#import "ImageLoader.h"


@implementation ImageLoader
@synthesize path, connection, data, loading, imageView;

- (id)init
{
    self = [super init];
    [self setUserInteractionEnabled:YES];
    return self;
}

- (void)loadImageFromUrl:(NSString *)URL forPath:(NSString *)chemin
{
    //if (connection != nil)    [connection release];
    //if (data !=  nil)     [data release];
    //if (path != nil)      [path release];

    self.path = chemin;

    if ([[NSFileManager defaultManager] fileExistsAtPath:chemin])
    {
        if (imageView != nil)
        {
            [imageView removeFromSuperview];
            [imageView release];
        }
        imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        imageView.image = [UIImage imageWithContentsOfFile:chemin];
        [self addSubview:imageView];
    }
    else 
    {
        loading = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)];
        loading.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        [loading setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [loading startAnimating];
        [self addSubview:loading];

        NSURL *myURL = [NSURL URLWithString:[URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];      
        NSURLRequest *request = [NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    }
}

#pragma mark -
#pragma mark NSURLConnection protocol

- (void)connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data
{
    if (data == nil)
    {
        data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:_data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)_connection
{
    [data writeToFile:self.path atomically:YES];

    if (imageView != nil)
    {
        [imageView removeFromSuperview];
        [imageView release];
    }

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
    imageView.frame = self.bounds;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    [loading stopAnimating];
    [loading setHidden:YES];
    [self addSubview:imageView];
}

- (void)connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error
{
    [loading stopAnimating];
    [loading release];
}

#pragma mark - Memory management

- (void)dealloc
{
    [connection cancel];
    [connection release];
    [imageView release];
    [path release];
    [data release];
    [super dealloc];
}

@end
于 2012-04-26T18:28:51.933 回答
1

最简单的方法是使用AFNetworking,它setImageWithURL:placeholderImage:在一个类别中提供 on UIImageView(也是一个带有成功/失败处理程序的方法)。

于 2012-04-26T18:38:31.290 回答