让我先解释一下场景。我有一个 UIScrollView
并在其中添加一些 UIButton
作为 subView
。在这些按钮中,我正在加载一些图像(来自远程服务器)。我使用 NSMutableURLRequest
加载所有图像并保存在 NSCache
中。由于加载需要很长时间,这就是为什么我想使用这个NSCache
,这样在其他ViewController
中,我就不必从远程服务器再次加载它们。因此,我通过 Segue
将 NSCache
传递给另一个 ViewController
。不过,我不确定 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
时,没有得到任何值。我想我在这里遗漏了一些东西。或者没有遵循适当的方式。
最佳答案
In SecondViewController I create NSCache *imageCache; again
不需要
self.imageCache = [[NSCache alloc] init];
去掉这一行,应该可以了
关于ios - NSCache UIImage 数据从一个 ViewController 传递到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25738733/