当我尝试为头像加载远程图像时,应用程序崩溃并显示错误,说我的图像不能为零。我只是暂时在我的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];
}
应用程序在进入成功日志之前崩溃,显然加载Web服务的消息没有被调用。
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'
答案 0 :(得分:1)
这是因为您的图像在视图需要时未加载。这可以通过使用默认图像来解决,直到您可以恢复图像。您可以创建一种方法,为您提供最初使用的默认空白头像,然后如果您的请求返回更新该单元格的头像。
func getBlankAvatar() -> UIImage {
let blankAvatar = JSQMessagesAvatarImageFactory.avatarImageWithUserInitials("?", backgroundColor: .gray, textColor: .white, font: UIFont.systemFontOfSize(14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
同样作为优化,如果您为对话中的用户创建字典并为其获取远程图像并将其保存到此字典,那么您只需要发出一次请求,并可以从该字典中提取单个消息单元格
答案 1 :(得分:0)
您需要先下载图像,然后才能使用它。您可以使用SDWebImage来处理Web图像的所有操作。 通过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];
}
}];