加载远程图像会破坏 JSQMessagesAvatarImageFactory

Loading a remote image is breaking JSQMessagesAvatarImageFactory

当我尝试为头像加载远程图像时,应用程序崩溃并出现错误,提示我的图像不能为零。我暂时只是在我的 ViewDidLoad 方法中添加头像创建

- (void)viewDidLoad {
    [super viewDidLoad];

    self.inputToolbar.contentView.textView.pasteDelegate = self;
    self.userAvatarDictionary = [[NSMutableDictionary alloc] init];
    self.latestGuid = @"";

    /**
     *  Load up our fake data for the demo
     */
    self.demoData = [[DemoModelData alloc] init];

    /**
     *  You can set custom avatar sizes
     */

    self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeMake(30, 30);

    self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage jsq_defaultTypingIndicatorImage]
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(receiveMessagePressed:)];


    //Load remote image for Gibson Les Paul Guitar as test
    NSString *imgURLString = @"http://images.gibson.com/Products/Electric-Guitars/Les-Paul/Gibson-USA/Les-Paul-Studio/Splash-02.jpg";

    NSURL *url = [NSURL URLWithString:imgURLString];
    NSLog(@"CREATED USER ID: 13");
    NSLog(@"CREATOR IMAGE URL: %@", imgURLString);
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *dataLoadedImg = [UIImage imageWithData:data];


    JSQMessagesAvatarImage *img = [JSQMessagesAvatarImageFactory avatarImageWithImage:dataLoadedImg diameter:20.0f];

    [self.userAvatarDictionary setObject:img forKey:@"13"];
    NSLog(@"ADDED GIBSON ICON SUCCESSFULLY");
    [self callViewMessagesListWithLatestMessageGuid:self.latestGuid
                                    CompletionBlock:^(NSMutableArray *resultsArray) {
                                        [self reloadData:resultsArray];
                                    }];
    [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(fifteenSecondsPassed:) userInfo:nil repeats:YES];
}

应用程序在进入成功日志之前崩溃了,显然消息加载网络服务没有被调用。

Logging/Error messaging:

CREATED USER ID: 13
CREATOR IMAGE URL: http://images.gibson.com/Products/Electric-Guitars/Les-Paul/Gibson-USA/Les-Paul-Studio/Splash-02.jpg

*** Assertion failure in +[JSQMessagesAvatarImageFactory jsq_circularImage:withDiameter:highlightedColor:], /Users/propstm/Documents/Github/MyProject/Pods/JSQMessagesViewController/JSQMessagesViewController/Factories/JSQMessagesAvatarImageFactory.m:132
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: image != nil'

这是因为您的图像没有在视图需要时加载。这可以通过使用默认图像来解决,直到您可以恢复图像。您可以创建一个方法来为您提供最初使用的默认空白头像,然后如果您的请求 returns 更新该单元格的头像。

func getBlankAvatar() -> UIImage {
   let blankAvatar =  JSQMessagesAvatarImageFactory.avatarImageWithUserInitials("?", backgroundColor: .gray, textColor: .white, font: UIFont.systemFontOfSize(14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))

如果您为对话中的用户创建一个字典并为他们获取远程图像并将其保存到该字典中,那么作为一种优化,您只需发出一次请求,就可以从该字典中为您的个人提取消息单元格。

您需要先下载图像,然后才能使用它。您可以使用 SDWebImage 来处理网络图像的所有操作。 通过 cocoapods 安装 SDWebImage 然后添加这些代码:

  [[SDWebImageManager sharedManager] downloadImageWithURL:imgURLString
                                              options:0
                                             progress:nil
                                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    if (!error && finished && image) {
    [self.userAvatarDictionary setObject:image forKey:@"13"];
    NSLog(@"ADDED GIBSON ICON SUCCESSFULLY");
    [self.collectionView reloadData];
  }
}];