让我先解释一下情景。我有一个UIScrollView
并在其中添加一些UIButton
作为subView
。在这些按钮中我加载了一些图像(来自远程服务器)。我使用NSMutableURLRequest
加载所有图片并保存在NSCache
中。因为加载需要很长时间,这就是我想要使用此NSCache
的原因,因此在其他ViewController
中,我不必再从远程服务器加载它们。因此,我将NSCache
传递给另一个ViewController
Segue
。不过,我不确定是否可以使用NSCache
?请让我知道。我在下面给你我的代码,以便你可以看看它。
提前多多感谢。祝你有愉快的一天。
MainViewController.h
@interface MainViewController : UIViewController
{
NSTimer *timer;
int counter;
}
@property (strong, nonatomic) IBOutlet UIButton *adButtonOutLet;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) AdInfo *currentAd;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) AdParser *adParser;
@property (nonatomic, strong) NSMutableArray *adsListArray;
@property (nonatomic, strong) NSMutableArray *displayArray;
@property (nonatomic, strong) NSCache *imageCache;
的 MainViewController.m
-(void)startAnimation:(id)data
{
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(140.0, 525.0, 40.0, 40.0);
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
activityIndicator.hidesWhenStopped=YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startAnimation:nil];
self.imageCache = [[NSCache alloc] init];
[self performSelector:@selector(loadData) withObject:nil afterDelay:0.5];
counter = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self selector: @selector(handleTimer:) userInfo: nil repeats: YES];
}
- (void)handleTimer:(NSTimer *)timer
{
//NSLog(@"counter %i", counter);
if (counter == [displayArray count])
{
[scrollView setContentOffset:CGPointMake(320, 0) animated:YES];
counter = 0;
}
else
{
[scrollView setContentOffset:CGPointMake(counter*320, 0) animated:YES];
}
counter++;
}
-(void) loadData
{
adParser = [[AdParser alloc] loadXMLByURL:getXMLURL];
adsListArray = [adParser ads];
displayArray = [[NSMutableArray alloc] init];
for (AdInfo *adInfo1 in adsListArray)
{
AdInfo *adInfo2 = [[AdInfo alloc] init];
[adInfo2 setBannerIconURL:adInfo1.bannerIconURL];
[adInfo2 setBannerIconLink:adInfo1.bannerIconLink];
[displayArray addObject:adInfo2];
}
[self loadScrollView];
[activityIndicator stopAnimating];
}
-(void) loadScrollView
{
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake([displayArray count] * ScrollerWidth, ScrollerHight)];
for (int i = 0; i < [displayArray count]; i++)
{
adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(i*320, 0, ButtonWidth, ButtonHight)];
currentAd = [displayArray objectAtIndex:i];
NSString *path = currentAd.bannerIconURL;
NSURL *url = [NSURL URLWithString:path];
NSMutableURLRequest *requestWithBodyParams = [NSMutableURLRequest requestWithURL:url];
NSData *imageData = [NSURLConnection sendSynchronousRequest:requestWithBodyParams returningResponse:nil error:nil];
UIImage *originalImage = [UIImage imageWithData:imageData];
UIImage *cachedImage = [self.imageCache objectForKey:currentAd.bannerIconURL];
if (cachedImage)
{
[adButtonOutLet setImage:cachedImage forState:UIControlStateNormal];
}
else
{
[self.imageCache setObject:originalImage forKey:currentAd.bannerIconURL];
NSLog(@"tulon %@", self.imageCache);
[adButtonOutLet setImage:originalImage forState:UIControlStateNormal];
}
adButtonOutLet.userInteractionEnabled= YES;
[adButtonOutLet setTag:i];
[adButtonOutLet addTarget:self action:@selector(goToURL:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:adButtonOutLet];
}
}
-(IBAction)goToURL:(UIButton *)sender
{
NSInteger indexValue = sender.tag;
for (int i = 0; i < [displayArray count]; i++)
{
if (indexValue == i)
{
currentAd = [displayArray objectAtIndex:i];
NSString *url = currentAd.bannerIconLink;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"goToSecondViewController"])
{
SecondViewController *secondViewController = [segue destinationViewController];
[secondViewController setImageCache:imageCache];
[secondViewController setDisplayArray:displayArray];
}
}
的 SecondViewController.h
@interface SecondViewController : UIViewController
{
NSTimer *timer;
int counter;
}
@property (strong, nonatomic) IBOutlet UIButton *adButtonOutLet;
@property (nonatomic, strong) AdInfo *currentAd;
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) NSCache *imageCache;
@property (nonatomic, strong) NSMutableArray *displayArray;
的 SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageCache = [[NSCache alloc] init];
[self performSelector:@selector(loadScrollView) withObject:nil afterDelay:0.5];
counter = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self selector: @selector(handleTimer:) userInfo: nil repeats: YES];
NSLog(@"tulon %@", self.imageCache);
}
- (void)handleTimer:(NSTimer *)timer
{
if (counter == [displayArray count])
{
[scrollView setContentOffset:CGPointMake(320, 0) animated:YES];
counter = 0;
}
else
{
[scrollView setContentOffset:CGPointMake(counter*320, 0) animated:YES];
}
counter++;
}
-(void) loadScrollView
{
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake([displayArray count] * ScrollerWidth, ScrollerHight)];
for (NSInteger i = 0; i < [displayArray count]; i++)
{
adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(i*320, 0, ButtonWidth, ButtonHight)];
currentAd = [displayArray objectAtIndex:i];
UIImage *cachedImage = [self.imageCache objectForKey:currentAd.bannerIconURL];
[adButtonOutLet setImage:cachedImage forState:UIControlStateNormal];
adButtonOutLet.userInteractionEnabled= YES;
[adButtonOutLet setTag:i];
[adButtonOutLet addTarget:self action:@selector(goToURL:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:adButtonOutLet];
}
}
-(IBAction)goToURL:(UIButton *)sender
{
NSInteger indexValue = sender.tag;
for (int i = 0; i < [displayArray count]; i++)
{
if (indexValue == i)
{
currentAd = [displayArray objectAtIndex:i];
NSString *url = currentAd.bannerIconLink;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
}
增加:
在SecondViewController
我再次创建NSCache *imageCache;
,当我NSlog
为它时,我没有获得任何价值。我想我在这里失踪了。或者不遵循适当的方式。
答案 0 :(得分:2)
在SecondViewController中,我创建了NSCache * imageCache;再次
不需要
self.imageCache = [[NSCache alloc] init];
删除此行,它应该可以正常工作