[英]Image is not loading on UIImageView
我在带有缩略图的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 *),并尝试用主Image替换我的customImageView类中的图像,但没有任何显示。 我在这里想念什么?
我猜您想为UIImageView构建某种缓存机制。 完成所有这些工作的最简单方法之一就是利用AFNetworking库。
AFNetworking扩展了UIImageView,并提供了一些其他方法来从远程源加载图像。 AFNetworking具有内置缓存功能,无需重新设计轮子:)
看一下以下站点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.