2012-07-24 156 views
0

我使用縮略圖圖像在UITableView中顯示數據。最初,當單擊單元格時,我將從服務器重新加載圖像,這會花費額外的時間,資源和內存。我並沒有這樣做,而是想將縮略圖圖像的指針傳遞給屏幕上的視圖並立即顯示,而不必重新下載它。但是,我遇到了一個問題 - 當我將新視圖上的imageView設置爲舊圖像時,什麼也沒有加載。這裏是我的代碼:圖像未在UIImageView上加載

這是我的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類替換主圖像中的圖像,但沒有顯示向上。我在這裏錯過了什麼?

+0

我不認爲你需要在此處分配/初始化映像: 'mainImage = [[UIImage alloc] init]; mainImage = image;' – Marty 2012-07-24 23:34:14

+0

我試着不分配/啓動它,我試圖直接設置它相等,我甚至嘗試initWithCGImage並從圖像獲取CGImage。他們都沒有工作 – kamran619 2012-07-24 23:40:23

+0

是的,我認爲它不會幫助任何東西,只是通過代碼注意到它。 – Marty 2012-07-24 23:51:27

回答

1

我想你想爲你的UIImageView建立某種緩存機制。使所有這些工作最簡單的方法之一是通過使用AFNetworking庫。

AFNetworking擴展UIImageView與一些額外的方法從遠程來源加載圖像。 AFNetworking具有緩存內置的,沒有必要推倒重來:)

採取以下網站看看:

+0

最糟糕的情況,我會用這個。我已經寫了大部分代碼,所以我寧願不必放棄它。你知道爲什麼它目前沒有工作嗎? – kamran619 2012-07-24 23:25:59

+0

也許你的圖像沒有存儲在你似乎用作緩存的字典中。你可以檢查調試器。 – 2012-07-24 23:33:44

+0

它顯示一個有效的內存地址。我還能如何檢查內容? – kamran619 2012-07-24 23:34:59