【问题标题】:SDWebImage in a simple test app crashes with NSInvalidArgumentException一个简单的测试应用程序中的 SDWebImage 因 NSInvalidArgumentException 而崩溃
【发布时间】:2014-01-30 17:15:45
【问题描述】:

在 Xcode 5.0.2 中

  1. 我为 iPhone 创建了a blank single view app
  2. 然后将“male.png”图片添加到项目中,
  3. 将 UIImageView 拖到情节提要
  4. 最后将以下代码添加到viewDidLoad

    _imageView.image = [UIImage imageNamed:@"male.png"];

这很好用:

然后我添加来自SDWebImage project 的文件并将ViewController.m 更改为:

#import "ViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

static NSString* const kAvatar = @"http://gravatar.com/avatar/55b3816622d935e50098bb44c17663bc.png";

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_imageView setImageWithURL:[NSURL URLWithString:kAvatar]
               placeholderImage:[UIImage imageNamed:@"male.png"]];
}

@end

很遗憾,这会导致应用崩溃并显示错误消息:

2014-01-11 19:21:57.731 sdImage[2563:70b] -[UIImageView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 0x8a5aa80
2014-01-11 19:21:57.745 sdImage[2563:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 0x8a5aa80'
*** First throw call stack:
(
    0   CoreFoundation                      0x017395e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014bc8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017d6903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0172990b ___forwarding___ + 1019
    4   CoreFoundation                      0x017294ee _CF_forwarding_prep_0 + 14
    5   sdImage                             0x00002b43 -[ViewController viewDidLoad] + 227
    6   UIKit                               0x0033e318 -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x0033e5b4 -[UIViewController view] + 35
    8   UIKit                               0x002669fd -[UIWindow addRootViewControllerViewIfPossible] + 66
    9   UIKit                               0x00266d97 -[UIWindow _setHidden:forced:] + 312
    10  UIKit                               0x0026702d -[UIWindow _orderFrontWithoutMakingKey] + 49
    11  UIKit                               0x0027189a -[UIWindow makeKeyAndVisible] + 65
    12  UIKit                               0x00224cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
    13  UIKit                               0x002293a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    14  UIKit                               0x0023d87c -[UIApplication handleEvent:withNewEvent:] + 3447
    15  UIKit                               0x0023dde9 -[UIApplication sendEvent:] + 85
    16  UIKit                               0x0022b025 _UIApplicationHandleEvent + 736
    17  GraphicsServices                    0x036e02f6 _PurpleEventCallback + 776
    18  GraphicsServices                    0x036dfe01 PurpleEventCallback + 46
    19  CoreFoundation                      0x016b4d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    20  CoreFoundation                      0x016b4a9b __CFRunLoopDoSource1 + 523
    21  CoreFoundation                      0x016df77c __CFRunLoopRun + 2156
    22  CoreFoundation                      0x016deac3 CFRunLoopRunSpecific + 467
    23  CoreFoundation                      0x016de8db CFRunLoopRunInMode + 123
    24  UIKit                               0x00228add -[UIApplication _run] + 840
    25  UIKit                               0x0022ad3b UIApplicationMain + 1225
    26  sdImage                             0x00002ffd main + 141
    27  libdyld.dylib                       0x01d7770d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

请问为什么会这样?添加框架文件时,我设置了复选框以复制文件。

更新:

我还尝试了以下方法:

git submodule add https://github.com/rs/SDWebImage.git

然后构建该项目 (SDWebImage.xcodeproj) 并关闭它。

然后我在 Xcode 中重新打开了我的项目 (sdImage.xcodeproj) 并将 SDWebImage.xcodeproj 文件拖入其中。

之后我尝试添加目标依赖项,但它没有显示任何名为“SDWebImage”的内容:

这里是full sized screenshot

我无法在 Link Binary With Libraries 下添加“SDWebImage.framework”,因为那里没有名为“SDWebImage”的内容。

【问题讨论】:

  • 尝试将以下内容添加到构建设置中的“其他链接器标志”中:“-force_load ${BUILT_PRODUCTS_DIR}/libSDWebImageARC.a”
  • 当我从 github.com/rs/SDWebImage/releases 下载任何 zip 档案时,那里没有 libSDWebImageARC.a 文件。只有几个标题和一个SDWebImage 文件。
  • 我不明白你的问题是什么。 github 中的项目运行良好并在其中加载您的 avtar
  • +1 用于查看我的项目。现在可以(从几分钟开始),因为我终于想出了如何在 CocoaPods 的帮助下将 SDWebImage.framework 集成到我的项目中 - 请在下面查看我自己的答案。

标签: ios iphone objective-c ipad uiimageview


【解决方案1】:

为什么不在主项目中包含 SDWebImage 源文件?这就是我合并 SDWebImage 的方式,SDWebImage 文档本身声明:“在您的项目中有两种使用它的方法:将所有文件复制到您的项目中,或者将项目作为静态库导入。 "如果您这样做,则无需打开 SDWebImage 项目、编译框架、将框架与您的项目链接等。

【讨论】:

  • 因为……我还没想过! :-) 我会试试你的建议,谢谢
  • 我已经尝试了你的超级简单的建议,它刚刚奏效:github.com/afarber/ios-newbie/tree/master/sdImageCopied
  • 哈哈,很高兴它成功了!老实说,我自己也是一个 iOS 新手,由于我的解决方案非常简单,我想我一定遗漏了一些东西。我几乎没有分享它。 :)
【解决方案2】:

我通过使用 CocoaPods 获得了该项目,并带有以下 Podfile

platform :ios, '5.0'
pod 'SDWebImage'

然后在 Xcode 中运行 pod install 并打开 sdImage.xcworkspace

我仍然对如何在项目中使用 SDWebImage 感兴趣——不使用 CocoaPods...

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2012-05-28
    • 2012-10-25
    • 2018-12-11
    • 1970-01-01
    • 2013-02-03
    • 2022-01-02
    • 2023-03-14
    相关资源
    最近更新 更多