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