简体   繁体   中英

SDWebImage in iOS Today Extension

I'm writing an iOS Today Extension that has some UIImageView s in it. I want to set images from an url in them and so I thought using SDWebImage would be best. I wrote the code below:

#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

However, this keeps crashing, throwing this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView sd_setImageWithURL:]: unrecognized selector sent to instance 0x7a88cc30'

First, remove following:

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

You never have to import implementation lines, only headers. In general principle of OOP you should hide implementation from other classes, thats called encapsulation.

Second, import #import <SDWebImage/UIImageView+WebCache.h>

That file have declaration of your setImageWithUrl method.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM