Я пытаюсь загрузить изображение на сервер. Но я получаю сообщение об ошибке -
Завершение работы приложения из-за необработанного исключения 'NSInvalidArgumentException', причина: '- [__ NSCFString CGImage]: нераспознанный селектор, отправленный экземпляру 0x281a1d050' *** Стек первого вызова вызова:
libc ++ abi.dylib: завершается с необработанным исключением типа NSException
*+(void)uploadImage: (NSArray *_Nullable)imagesArr productId: (NSString *_Nullable)productId featured:(BOOL)featured downloadProgress:(nullable void (^)(NSProgress * _Nullable downloadProgress))downloadProgressBlock completionHandler:(nullable void(^)(NSError * _Nullable error,id _Nullable response))completionHandler{
REQUEST_TYPE type = REQUEST_TYPE_JSON;
REQUEST_METHOD method = REQUEST_METHOD_POST;
NSMutableDictionary *headerDict = [[NSMutableDictionary alloc] init];
[headerDict setObject:[NSString stringWithFormat:@"Bearer %@",GET_TOKEN] forKey:@"Authorization"];
[headerDict setObject:[NSString stringWithFormat:@"application/json"] forKey:@"Content-Type"];
ServerConnection *connection = [ServerConnection getInstance];
NSString *createProducAPIURL = [NSString stringWithFormat:@"%@%@",URL_BASE,URL_PRODUCTIMAGE_UPLOAD];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
//
NSMutableArray *imageDictArr = [[NSMutableArray alloc] init];
int position = 0;
for (NSString *url in imagesArr) {
NSMutableDictionary *imageDict = [[NSMutableDictionary alloc] init];
[imageDict setObject:url forKey:@"src"];
[imageDict setObject:[NSNumber numberWithInt:position] forKey:@"position"];
[imageDictArr addObject:imageDict];
position++;
}
NSData *imageData = UIImageJPEGRepresentation(imagesArr[0], 0.5);
NSString *encodedString = [imageData base64EncodedStringWithOptions:kNilOptions];
NSLog(@"image: %@", imageData);
[parameters setObject:productId forKey:@"product_id"];
[parameters setObject:encodedString forKey:@"file"];
[connection dataTask:createProducAPIURL parameters:parameters requestType:type method:method headerDict:(NSDictionary *_Nullable)headerDict uploadProgress:^(NSProgress * _Nullable serveruploadProgress) {
} downloadProgress:^(NSProgress * _Nullable downloadProgress) {
downloadProgressBlock(downloadProgress);
} completionHandler:^(id _Nullable jsonSerializedData, NSError * _Nullable error) {
completionHandler(error,jsonSerializedData);
}];
}
-(void) dataTask:(NSString *_Nullable)URLString parameters:(NSDictionary *_Nullable)parameters requestType:(REQUEST_TYPE)type method:(REQUEST_METHOD)method headerDict:(NSDictionary *_Nullable)headerDict uploadProgress:(nullable void (^)(NSProgress * _Nullable uploadProgress))uploadProgressBlock downloadProgress:(nullable void (^)(NSProgress * _Nullable downloadProgress))downloadProgressBlock completionHandler:(nullable void (^)(id _Nullable jsonSerializedData, NSError * _Nullable error))completionHandler
{
NSMutableURLRequest *request;
// URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:set];
switch (type) {
case REQUEST_TYPE_NORMAL:
request = [[AFHTTPRequestSerializer serializer] requestWithMethod:[self requestMethod:method] URLString:URLString parameters:parameters error:nil];
break;
case REQUEST_TYPE_JSON:
{
request = [[AFJSONRequestSerializer serializer] requestWithMethod:[self requestMethod:method] URLString:URLString parameters:nil error:nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];
if(!error)
{
[request setHTTPBody:jsonData];
NSLog(@"Request body %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
}
else{
completionHandler(nil,error);
return;
}
}
break;
default:
request = [[AFHTTPRequestSerializer serializer] requestWithMethod:[self requestMethod:method] URLString:URLString parameters:parameters error:nil];
break;
}
if(!isNullObject(headerDict))
[request setAllHTTPHeaderFields:headerDict];
NSLog(@"TOKEN:%@",[USER_DEFAULTS objectForKey:kKeyToken]);
NSLog(@"REQUEST URL:%@",URLString);
NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
NSURLSessionDataTask *dataTask = [_manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"Upload Progress: %f",uploadProgress.fractionCompleted);
uploadProgressBlock(uploadProgress);
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"downloadProgress : %f",downloadProgress.fractionCompleted);
downloadProgressBlock(downloadProgress);
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if(!error)
{
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
// NSInteger statusCode = [HTTPResponse statusCode];
NSLog(@"URL:%@",HTTPResponse.URL);
NSLog(@"response:%@",string);
if([string rangeOfString:@"<!DOCTYPE html>"].location != NSNotFound){
string = [self stringByStrippingHTML:string];
NSData *resObj = [string dataUsingEncoding:NSUTF8StringEncoding];
id responseObject1 = [NSJSONSerialization JSONObjectWithData:resObj options:NSJSONReadingAllowFragments error:&error];
completionHandler(responseObject1,error);
}
else{
id responseObject1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
completionHandler(responseObject1,error);
}
}
else{
NSLog(@"error:%@",error);
id responseObject2 = nil;
if(responseObject!=nil)
{
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"response:%@",string);
responseObject2 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
}
completionHandler(responseObject2,error);
}
}];
[dataTask resume];
}*