抓住特定的ABPerson图像时崩溃

时间:2012-12-05 15:47:52

标签: ios json abaddressbook abperson

我正在iOS通讯录/ Contacts.app中抓取用户联系人的图片。并将它们放在字典中以上传为JSON。

我收到以下错误:

2012-12-05 10:38:01.286 ContactsApp[6247:713f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSCFData)'
*** First throw call stack:
(0x2de6012 0x286ce7e 0x2de5deb 0x20926fe 0x2096b21 0x2dd3cdf 0x2dd387d 0x2dd37c5 0x20966fa 0x209262d 0x2096b21 0x2dd3cdf 0x2dd387d 0x2dd37c5 0x20966fa 0x209262d 0x20969af 0x2ddfe7c 0x2ddfa16 0x2ddf925 0x20968b8 0x2092679 0x2096b21 0x2dd3cdf 0x2dd387d 0x2dd37c5 0x20966fa 0x209262d 0x20923bd 0x209579c 0x1cad5 0x67475 0x66a87 0x14399cd 0x746008f 0x3dc253f 0x3dd4014 0x3dc52e8 0x3dc5450 0x90e36e12 0x90e1ecca)
libc++abi.dylib: terminate called throwing an exception

我使用以下代码:

if (ABPersonHasImageData(addressBookContact)) {
    NSMutableDictionary *imageDictionary = [NSMutableDictionary dictionary];
    NSData *thumbnailImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(addressBookContact, kABPersonImageFormatThumbnail);
    NSData *originalImageData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(addressBookContact, kABPersonImageFormatOriginalSize);
    if (thumbnailImageData) [imageDictionary setObject:thumbnailImageData forKey:@"thumbnailImage"];
    if (originalImageData) [imageDictionary setObject:originalImageData forKey:@"originalImage"];
    [contactDictionary setObject:imageDictionary forKey:@"image"];
}

当我尝试将数组放入此请求时发生错误:

[addressBookArray addObject:contactDictionary];
if ([addressBookArray count] % 15 == 0) {
    // I'm using AFNetworking
    [[APIClient sharedClient] requestWithMethod:@"POST" path:@"cmd/addContact" parameters:@{ @"addressBookEntries" : addressBookArray }];
    [addressBookArray removeAllObjects];
}

2 个答案:

答案 0 :(得分:2)

您的问题是您正在尝试将NSData对象放入JSON对象中。而不是将图像数据添加到imageDictionary,将图像的base64编码添加到imageDictionary,你应该没有问题。

Matt Gallagher在这里处理base64有一个方便的课程:http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

答案 1 :(得分:0)

看起来AFNetworking正在尝试将您的参数解析为JSON,但您传递的是NSData。虽然你传递的NSDictionary是有效的,但那些嵌套类型需要是其他NSDictionaries,NSArrays,NSStrings和NSNumbers。如果要附加图像NSDatas,则必须使用不同的内容类型并附加图像数据。