让我先解释一下场景。我有一个UIScrollView
and 在里面,添加一些UIButton
as subView
。在这些按钮中,我正在加载一些图像(来自远程服务器)。我加载所有图像NSMutableURLRequest
并保存在NSCache
. 由于加载需要很长时间,这就是我想使用 this 的原因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