2012-07-24 64 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