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];

新闻视图控制器.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;
    }
}

新闻视图控制器.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 *) 并尝试用主 Image 替换我的 customImageView 类中的图像,但没有显示任何内容。我在这里想念什么?

4

1 回答 1

1

我猜你想为你的 UIImageView 构建某种缓存机制。完成所有这些工作的最简单方法之一是使用 AFNetworking 库。

AFNetworking 通过一些额外的方法扩展 UIImageView 来从远程源加载图像。AFNetworking 内置缓存,无需重新发明轮子 :)

看看以下网站:

于 2012-07-24T23:16:35.383 回答