gpt4 book ai didi

ios - UIImage(从路径)到 NSData 导致崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:40:09 25 4
gpt4 key购买 nike

我有以下代码

for (int i=0; i<self.imageArray.count; i++) 
{
[self.operation addData:[self getDataFromImage:[self.imageArray objectAtIndex:i]]
forKey:[NSString stringWithFormat:@"pa_image%d",i+1]
mimeType:@"image/png"
fileName:[NSString stringWithFormat:@"pa_image%d.png",i+1]];

}

从这里调用下面的方法

-(NSData *)getDataFromImage:(UIImage *)image
{
NSData *imageData= UIImageJPEGRepresentation(image, 0.1);
return imageData;
}

在我的 imageArray 代码中有 2 种情况

1) 值来自 imageArray 中的 API,例如“images/upload/d872f1.png”、“images/upload/0fdade.png”,然后在 NSData 中返回 nil。我想从此路径转换为 ​​NSData。怎么办?

执行此行后,我的应用程序崩溃了。这是给

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString CGImage]: unrecognized selector sent to instance 0x7f83a1f13510'

2) 当我从手机上传时,它像“,{834, 1250}”。我的代码在这种情况下正常工作,它转换为 NSData。

有关更多详细信息,请参阅我的 addData 方法

-(void) addData:(NSData*) data forKey:(NSString*) key mimeType:(NSString*) mimeType fileName:(NSString*) fileName 
{

if ([self.request.HTTPMethod isEqualToString:@"GET"])
{
[self.request setHTTPMethod:@"POST"];
}

NSDictionary *dict = @{@"data": data,
@"name": key,
@"mimetype": mimeType,
@"filename": fileName};

[self.dataToBePosted addObject:dict];
}

最佳答案

Try This

for (int i=0; i<self.imageArray.count; i++)
{
UIImage *image;
if([[self.imageArray objectAtIndex:i] isKindOfClass:[NSString class]]){
image = [UIImage imageNamed:[self.imageArray objectAtIndex:i]];
}else{
image = [self.imageArray objectAtIndex:i];
}
[self.operation addData:[self getDataFromImage:image]
forKey:[NSString stringWithFormat:@"pa_image%d",i+1]
mimeType:@"image/png"
fileName:[NSString stringWithFormat:@"pa_image%d.png",i+1]];

}

关于ios - UIImage(从路径)到 NSData 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175684/

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