我在尝试将缩略图从JSON加载到UITableView单元格时遇到问题。单元格的UI是一个xib文件。我无法解析JSON的缩略图部分并将其显示在表视图中。我做了这个已经有一段时间了(我倾向于使用比典型的iOS应用程序更多的游戏),而我最后一次做的是实现略有不同。
JSON:
{
"data" :
[
{
"user_id" : "3",
"username" : "Alex Perez",
"avatar_url" : "http://mywebsite.com/images/alex_avatar.png",
"message" : "Hello there?"
},
{
"user_id" : "4",
"username" : "Jon Doe",
"avatar_url" : "http://mywebsite.com/images/john_avatar.png",
"message" : "I'm here now"
},
{
"user_id" : "2",
"username" : "Sam Givens",
"avatar_url" : "http://mywebsite.com/images/sam_avatar.png",
"message" : "Can we have a meeting around 2?"
},
....
Data.h中的数据:
#import <Foundation/Foundation.h>
@interface Data : NSObject
@property (nonatomic, readwrite) int user_id;
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *avatar_url;
@property (nonatomic, strong) NSString *message;
- (void)loadWithDictionary:(NSDictionary *)dict;
@end
Data.m:
#import "Data.h"
@implementation Data
- (void)loadWithDictionary:(NSDictionary *)dict {
self.user_id = [[dict objectForKey:@"user_id"] intValue];
self.username = [dict objectForKey:@"username"];
self.avatar_url = [dict objectForKey:@"avatar_url"];
self.message = [dict objectForKey:@"message"];
}
@end
在声明属性并将属性分配给数据时,我遇到了问题,然后可以将其传递给将要显示它的View Controller。我收到编译器警告不兼容的指针类型从NSString *分配给UIImage *。我不明白为什么Xcode不抱怨UILabel和UITextView等其他属性。
Cell.m
#import "Cell.h"
@interface Cell ()
@property (nonatomic) int userID;
// UIImageView property created from Interface Builder
@property (strong, nonatomic) IBOutlet UIImageView *avatarImage;
@property (nonatomic, strong) IBOutlet UILabel *usernameLabel;
@property (nonatomic, strong) IBOutlet UITextView *messageTextView;
@end
@implementation Cell
- (void)loadWithData:(Data *)data {
self.userID = data.user_id;
// Here I get "Incompatible pointer types assigning to UIImage * from NSString*
self.avatarImage.image = data.avatar_url;
self.usernameLabel.text = data.username;
self.messageTextView.text = data.message;
}
@end
最后显示它的MessagesViewController:
@interface MessagesViewController ()
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *loadedChatData;
@end
@implementation MessagesViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.loadedChatData = [[NSMutableArray alloc] init];
[self loadJSONData];
}
- (void)loadJSONData {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSError *error = nil;
NSData *rawData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
id JSONData = [NSJSONSerialization JSONObjectWithData:rawData options:NSJSONReadingAllowFragments error:&error];
[self.loadedChatData removeAllObjects];
if ([JSONData isKindOfClass:[NSDictionary class]]) {
NSDictionary *jsonDict = (NSDictionary *)JSONData;
NSArray *loadedArray = [jsonDict objectForKey:@"data"];
if ([loadedArray isKindOfClass:[NSArray class]])
{
for (NSDictionary *chatDict in loadedArray)
{
Data *chatData = [[Data alloc] init];
[chatData loadWithDictionary:chatDict];
[self.loadedChatData addObject:chatData];
}
}
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
Cell *cell = nil;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
cell = (Cell *)[nib objectAtIndex:0];
}
Data *data = [self.loadedChatData objectAtIndex:[indexPath row]];
[cell loadWithData:data];
return cell;
}
答案 0 :(得分:1)
你必须修改错误行并将其用作:
self.avatarImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:data.avatar_url]]];
答案 1 :(得分:0)
为笔尖创建自定义类,然后将特定代码放入其中以从直接URL加载图像...这可以
var stringUrl = "http://mywebsite.com/images/alex_avatar.png"
var url: NSURL = NSURL(string: stringUrl)!;
self.imageView1.sd_setImageWithURL(url, placeholderImage: UIImage(named: "no_image_small.png"));
答案 2 :(得分:0)
尝试使用以下代码,它可能会对您有所帮助。
NSURL *url = [NSURL URLWithString:@"http://mywebsite.com/images/alex_avatar.png"];
UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
if (thumbnail == nil) {
thumbnail = [UIImage imageNamed:@"noimage.jpg"] ; //if no image get from url mean show a dummy one
}
CGSize itemSize = CGSizeMake(80, 80); //your own size can replace with 80
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[thumbnail drawInRect:imageRect];
cell.thumbnailImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();