2012-04-26 10 views
0

私はiOS開発を初めて利用しています。 基本的なフォトギャラリーを作成しようとしていますが、問題が発生しました。 私がこのプロジェクトを始めたとき、私は自分のプロジェクトにすべてのイメージを含めました。 イメージをもっとたくさん(400+)した後、私はそれらをサーバーから読み込み始めました。 Iは、次のコード行使用して画像のアレイを作った:明らかサーバからロードする400枚の以上の画像のアレイのためのユーザ待ち時間を作るサーバーからイメージを読み上げる前に「読み込む」イメージをロードしますか?

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

は受け入れられません。

私のプロジェクトに「Loading」のようなイメージが1つ含まれていると、実際のイメージがサーバーからロードされるまでイメージを表示するにはどうすればよいですか?

私はテーブルビューとスクロールビューを使って基本的なグリッドスタイルのフォトギャラリーを作っています。それは小さな(サムネイル)画像の数行を読み込み、1つをクリックするとフルスクリーンになります。

Xcode 4.3、ARC、およびストーリーボードを使用しています。

ご迷惑をおかけして申し訳ありません。

-Shredder2794

答えて

1

これを行う最も簡単な方法は(も成功/失敗ハンドラを持つ方法)UIImageView上のカテゴリにsetImageWithURL:placeholderImage:を提供AFNetworkingを、使用することです。

+0

サードパーティのソフトウェアを使用しないことをお勧めします。 – Shredder2794

+0

なぜですか?私の知る限りでは、この特定のライブラリは尊敬され、多くのアプリで使用されています。なぜ車を再発明するのですか? (その理由とその理由を理解するのには不足しています) –

+0

あなたを惑わして申し訳ありませんが、私は実際に画像をボタンに追加しています。 AFNetorkingはsetImageWithURLでこれに対処しません。私はsetImageWithURLと呼ばれるUIImageViewを作成し、ボタンにイメージビューを追加しましたが、何もしません。他のアイデア? – Shredder2794

1

方法はUIImageViewをサブクラス化することです。あなたはそれを割り当てるときに、デフォルトイメージ(ローディング1)またはUIActivityIndicatorを入れて、表示したいイメージを(別のスレッドで)ダウンロードし、イメージがダウンロードされたら表示します。画像のダウンロードについてはNSURLRequestNSURLConnectionをご覧ください。

* EDIT *

ここでは、私が開発したコードの例です。これを開始点として使用して、独自の読み込みイメージクラスを開発することができます。このクラスは画像の読み込みに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 
+0

私はアイデアを得るが、私はまだこれを動作させる問題を抱えている。いくつかのサンプルコードを提供できますか? – Shredder2794

関連する問題