2015-04-04 70 views
1

我正在写一个iOS Today扩展,其中包含一些UIImageView s。我想从他们的网址设置图像,所以我认为使用SDWebImage将是最好的。我写了下面的代码:iOS今日扩展中的SDWebImage

#import "TodayViewController.h" 
#import <NotificationCenter/NotificationCenter.h> 
#import "UIImageView+WebCache.h" 
#import "SDImageCache.h" 
#import "UIImageView+WebCache.m" 
#import "SDImageCache.m" 

@interface TodayViewController() <NCWidgetProviding> 

@property (strong, nonatomic) UIImageView *firstImage; 

@property (strong, nonatomic) UILabel *titleLabel; 

@property (strong, nonatomic) NSDictionary *dataOne; 

@end 

@implementation TodayViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [self updateData]; 

    self.preferredContentSize = CGSizeMake(self.view.frame.size.width, 230); 

    NSInteger quarterSize = self.view.frame.size.width/4; 
    NSInteger eightSize = quarterSize/4; 

    self.firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(eightSize, 45, quarterSize, quarterSize*1.25)]; 
    [self.firstImage sd_setImageWithURL:[NSURL URLWithString:@"http://anluan.com/crest2.jpg"]]; 
    [self.view addSubview:self.firstImage]; 

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(eightSize, self.firstArticle.frame.origin.y + self.firstArticle.frame.size.height + 10, quarterSize, 20)]; 
    self.titleLabel.text = [self.dataOne objectForKey:@"title"]; 
    self.titleLabel.numberOfLines = 2; 
    self.titleLabel.textColor = [UIColor whiteColor]; 
    self.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13]; 
    [self.titleLabel sizeToFit]; 
    [self.view addSubview:self.titleLabel]; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    if (self = [super initWithCoder:aDecoder]) { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(userDefaultsDidChange:) 
               name:NSUserDefaultsDidChangeNotification 
               object:nil]; 
    } 
    return self; 
} 

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets 
{ 
    return UIEdgeInsetsZero; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler { 
    // Perform any setup necessary in order to update the view. 

    // If an error is encountered, use NCUpdateResultFailed 
    // If there's no update required, use NCUpdateResultNoData 
    // If there's an update, use NCUpdateResultNewData 

    completionHandler(NCUpdateResultNewData); 
} 

- (void)userDefaultsDidChange:(NSNotification *)notification { 
    [self updateNumberLabelText]; 
} 

- (void)updateNumberLabelText { 
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.TodayExtensionDefaults"]; 
    self.dataOne = [defaults objectForKey:@"dataOne"]; 
    } 
} 

@end 

然而,这总是崩溃,引发此错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView sd_setImageWithURL:]: unrecognized selector sent to instance 0x7a88cc30'

+0

我已经建立了一个测试项目,并且我已经添加了SDWebImage,它工作的很好。我可以看到来自给定网址的图像。 – Attiqe 2015-04-04 10:09:47

回答

0

首先,删除以下:

#import "UIImageView+WebCache.m" 
#import "SDImageCache.m" 

你从来没有进口实施的线条,只有头。在OOP的一般原则中,你应该隐藏其他类的实现,这就是所谓的封装。

二,进口#进口<SDWebImage/UIImageView+WebCache.h>

该文件有你setImageWithUrl方法的声明。

干杯。

+0

这仍然会引发相同的错误。 – user4486205 2015-04-04 10:10:01

+0

你有文件#import ?如果你有它,它不应该抛出异常,因为那个类已经声明了你的方法。 – 2015-04-04 10:11:44

+0

是的,我有。这与今日视图扩展的事实无关吗? – user4486205 2015-04-04 10:15:27