OStack程序员社区-中国程序员成长平台

标题: ios - UIImage(从路径)到 NSData 导致崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:33
标题: ios - UIImage(从路径)到 NSData 导致崩溃

我有以下代码

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 *)getDataFromImageUIImage *)image
{
    NSData *imageData=  UIImageJPEGRepresentation(image, 0.1);
    return imageData;
}

我的 imageArray 代码中有两种情况

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) addDataNSData*) data forKeyNSString*) key mimeTypeNSString*) mimeType fileNameNSString*) fileName 
 {

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

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

[self.dataToBePosted addObject:dict];
}



Best Answer-推荐答案


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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4