0

サムネイル画像を使用して UITableView にデータを表示しています。最初に、セルがクリックされると、サーバーから画像をリロードしますが、これには時間、リソース、およびメモリがさらにかかります。これを行う代わりに、サムネイル画像のポインターを画面に表示されるビューに渡して、再ダウンロードする代わりにすぐに表示することを考えました。ただし、問題が発生しています。新しいビューの imageView を古いイメージに設定すると、何も読み込まれません。これが私のコードです:

これは私の UITableView クラスにあります:

CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
NewsViewController *newsViewController = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil dictionary: dict type:d numberOfPages:1 image: cell.imageView.image];

NewsViewController.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictionary:(NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger)pages image:(UIImage *)image{
    self = [super initWithNibName:@"NewsViewController" bundle:Nil];
    if (self) {

        newsText = [dict objectForKey:@"content"];
        newsTitle = [dict objectForKey:@"title"];
        newsDate = [dict objectForKey:@"pub_date"];
        d = type;
        numberOfPages = pages;
        imageURL = [[NSMutableArray alloc] initWithCapacity:numberOfPages];
        mainImage = [[UIImage alloc] init];
        mainImage = image;
   }
   return self; 
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    labelBody.text= newsText;
    titleView.text = newsTitle;
    date.text = newsDate;
    if (numberOfPages == 1) {
        imageScrollView.contentSize = CGSizeMake(320, 303);
        pageControl.numberOfPages = 1;
        CustomImageView *customImageView  = [[CustomImageView alloc] initWithFrame:imageScrollView.bounds];
        [imageScrollView addSubview:customImageView];
        customImageView.imageView.image = mainImage;
    }
}

NewsViewController.h

#import <UIKit/UIKit.h>
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"

@class Reachability;

@interface NewsViewController : UIViewController<AdWhirlDelegate, UIScrollViewDelegate>{
    AdWhirlView *adView;
    IBOutlet UILabel *labelBody;
    IBOutlet UILabel *titleView;
    IBOutlet UILabel *subTitle;
    IBOutlet UILabel *date;
    IBOutlet UIScrollView *scrollView;
    IBOutlet UIScrollView *imageScrollView;
    IBOutlet UIImageView *backgroundImage;
    IBOutlet UIImage *mainImage;
    IBOutlet UIPageControl *pageControl;
    NSInteger numberOfPages;
    NSMutableArray *imageURL;

}

@property(nonatomic,retain)AdWhirlView *adView;
@property (nonatomic, retain) NSMutableArray *imageURL;  
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
@property (nonatomic, weak) IBOutlet UIImageView *backgroundImage;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIImage *mainImage;
@property (nonatomic, strong) NSString *newsText;
@property (nonatomic, strong) NSString *newsTitle;
@property (nonatomic, strong) NSString *newsDate;
@property (nonatomic, assign) NSInteger numberOfPages;
@property (nonatomic, strong) IBOutlet UILabel *titleView;
@property (nonatomic, strong) IBOutlet UILabel *labelBody;
@property (nonatomic, strong) IBOutlet UILabel *subTitle;
@property (nonatomic, strong) IBOutlet UILabel *date;
@property (nonatomic, retain) Reachability* internetReachable;
@property (nonatomic, assign) BOOL internetActive;

-(void) checkNetworkStatus:(NSNotification *)notice;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil     dictionary: (NSDictionary *)dict type:(NSInteger)type numberOfPages:(NSInteger) pages image:(UIImage *)image;
- (BOOL)checkInternet;
- (void)doStuff;
- (IBAction)ad;

@end

mainImage を (UIImage *) として使用し、customImageView クラスの画像をメインの画像に置き換えようとしていますが、何も表示されません。ここで何が欠けていますか?

4

1 に答える 1

1

UIImageView 用にある種のキャッシュ メカニズムを構築したいと思います。これらすべてを機能させる最も簡単な方法の 1 つは、AFNetworking ライブラリを利用することです。

AFNetworking は、リモート ソースから画像をロードするためのいくつかの追加メソッドで UIImageView を拡張します。AFNetworking にはキャッシングが組み込まれているため、車輪を再発明する必要はありません :)

以下のサイトをご覧ください。

于 2012-07-24T23:16:35.383 に答える