ios - 第一次运行时从 NSUserDefaults 将图像作为 NSData 加载失败

标签 ios objective-c nsuserdefaults unrecognized-selector

在 Xcode 5.0.2 中,我试图重新创建示例 iOS SDK: Working with NSUserDefaults Jeroen van Rijn 先生:

enter image description here

我已经签到my complete project at the Github ,例如这里是ViewController.m图片加载发生的地方:

- (void)viewDidLoad
{
    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *firstName = [defaults objectForKey:@"firstName"];
    NSString *lastName = [defaults objectForKey:@"lastname"];
    int age = [defaults integerForKey:@"age"];
    NSString *ageString = [NSString stringWithFormat:@"%i",age];
    NSData *imageData = [defaults dataForKey:@"image"];
    UIImage *contactImage = [UIImage imageWithData:imageData];
    // Update the UI elements with the saved data
    _firstNameTextField.text = firstName;
    _lastNameTextField.text = lastName;
    _ageTextField.text = ageString;
    _contactImageView.image = contactImage;
    [super viewDidLoad];
}

- (IBAction)save:(id)sender
{
    // Hide the keyboard
    [_firstNameTextField resignFirstResponder];
    [_lastNameTextField resignFirstResponder];
    [_ageTextField resignFirstResponder];
    // Create strings and integer to store the text info
    NSString *firstName = [_firstNameTextField text];
    NSString *lastName  = [_lastNameTextField text];
    int age = [[_ageTextField text] integerValue];
    // Create instances of NSData
    UIImage *contactImage = _contactImageView.image;
    NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:firstName forKey:@"firstName"];
    [defaults setObject:lastName forKey:@"lastname"];
    [defaults setInteger:age forKey:@"age"];
    [defaults setObject:imageData forKey:@"image"];
    [defaults synchronize];
    NSLog(@"Data saved");
}

不幸的是,当在 iPhone 模拟器中运行时,该应用程序失败并显示:

2013-12-20 10:15:25.417 MyDefaults[2213:70b] -[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00
2013-12-20 10:15:25.426 MyDefaults[2213:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00'
*** First throw call stack:
(
    0   CoreFoundation                      0x0173a5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bd8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017d7903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0172a90b ___forwarding___ + 1019
    4   CoreFoundation                      0x0172a4ee _CF_forwarding_prep_0 + 14
    5   MyDefaults                          0x000028e6 -[ViewController viewDidLoad] + 598
    6   UIKit                               0x0033f318 -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x0033f5b4 -[UIViewController view] + 35
    8   UIKit                               0x002679fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    9   UIKit                               0x00267d97 -[UIWindow _setHidden:forced:] + 312
    10  UIKit                               0x0026802d -[UIWindow _orderFrontWithoutMakingKey] + 49
    11  UIKit                               0x0027289a -[UIWindow makeKeyAndVisible] + 65
    12  UIKit                               0x00225cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    13  UIKit                               0x0022a3a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    14  UIKit                               0x0023e87c -[UIApplication handleEvent:withNewEvent:] + 3447
    15  UIKit                               0x0023ede9 -[UIApplication sendEvent:] + 85
    16  UIKit                               0x0022c025 _UIApplicationHandleEvent + 736
    17  GraphicsServices                    0x036e12f6 _PurpleEventCallback + 776
    18  GraphicsServices                    0x036e0e01 PurpleEventCallback + 46
    19  CoreFoundation                      0x016b5d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    20  CoreFoundation                      0x016b5a9b __CFRunLoopDoSource1 + 523
    21  CoreFoundation                      0x016e077c __CFRunLoopRun + 2156
    22  CoreFoundation                      0x016dfac3 CFRunLoopRunSpecific + 467
    23  CoreFoundation                      0x016df8db CFRunLoopRunInMode + 123
    24  UIKit                               0x00229add -[UIApplication _run] + 840
    25  UIKit                               0x0022bd3b UIApplicationMain + 1225
    26  MyDefaults                          0x0000376d main + 141
    27  libdyld.dylib                       0x01d7870d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

是不是因为 NSUserDefaults 中最初没有存储图像数据(作为 NSData 键为“image”)?

最佳答案

您正在尝试在 UIView 中设置图像,而不是 UIImageView :-) 所以不要认为您的 _contactImageView 不是 UIImageView 类型。查看 StackTrace 的第一行:

[UIView setImage:]: unrecognized selector sent to instance 0x8a6af00

关于ios - 第一次运行时从 NSUserDefaults 将图像作为 NSData 加载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700259/

相关文章:

objective-c - 为什么这些 ARC 对象的行为不一致?

macos - MacOS中窗口的全屏状态保存在哪里

ios - 具有 NSNumber 到 NSString 映射的 NSDictionary 是非属性列表对象

nsdictionary - NSUserDefaults:如何使用 NSString 键和 BOOL 值设置 NSDictionary?

objective-c - IOS 在 PDF 中嵌入字体

ios - 如何为 MapView 上的每个自定义注释添加图像?

c++ - NSString UTF8String 访问错误

iphone - 检测两个图像是否在 Objective-C 中发生碰撞并打印消息

ios - 单独的字符串 : 2 labels

ios - 如何为一对多关系 Core Data iOS 添加谓词