NSCache UIImage 数据从 oneViewController 传递到 anotherViewController
Posted
技术标签:
【中文标题】NSCache UIImage 数据从 oneViewController 传递到 anotherViewController【英文标题】:NSCache UIImage data passing from oneViewController to anotherViewController 【发布时间】:2014-09-09 07:16:29 【问题描述】:让我先解释一下场景。我有一个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
时,没有得到任何价值。我想我在这里遗漏了一些东西。或者没有遵循适当的方式。
【问题讨论】:
【参考方案1】:在 SecondViewController 我创建 NSCache *imageCache;再次
不需要
self.imageCache = [[NSCache alloc] init];
删除这一行,它应该可以工作
【讨论】:
我明白了。 :D 当我在SecondViewController
中alloc
和init
时,它会创建一个新的imageCache
。 :) Bohut shukriya。
完全正确。你说的:)以上是关于NSCache UIImage 数据从 oneViewController 传递到 anotherViewController的主要内容,如果未能解决你的问题,请参考以下文章