gpt4 book ai didi

ios - iOS:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON写入(_NSInlineData)中的类型无效”

转载 作者:行者123 更新时间:2023-12-01 18:13:48 26 4
gpt4 key购买 nike

NSData *imgData;
if (!_groupImageView.image)
{
imgData=[NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"Replay.png"])];
}
else
{
imgData=[NSData dataWithData:UIImagePNGRepresentation(_groupImageView.image)];
}
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL* url = [NSURL URLWithString:[ NSString stringWithFormat:@"%@groups/create",[sharedObj getSiteUrl]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys:sharedObj.acessToken, @"tocken", _txtGroupName.text, @"name",imgData, @"image", nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];

当我尝试对图像数据进行JSON编码时,出现此异常,

由于未捕获的异常而终止应用程序
'NSInvalidArgumentException',原因:'JSON写入中的类型无效
(_NSInlineData)'

最佳答案

或者使用base64EncodedString将imgData更改为字符串;

NSData *imgData;
if (!_groupImageView.image)
{
imgData=[NSData dataWithData:UIImagePNGRepresentation([UIImage imageNamed:@"Replay.png"])];
}
else
{
imgData=[NSData dataWithData:UIImagePNGRepresentation(_groupImageView.image)];
}

//将图像数据更改为base64EncodedString
NSString *base64=[imgData base64EncodedStringWithOptions:0];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL* url = [NSURL URLWithString:[ NSString stringWithFormat:@"%@groups/create",[sharedObj getSiteUrl]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];

//将base64添加为键“image”的值
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys:sharedObj.acessToken, @"tocken",  _txtGroupName.text, @"name",base64, @"image",  nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];

关于ios - iOS:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON写入(_NSInlineData)中的类型无效”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com