// 新建文件夹
NSString *drivingImagePath = [self imageSavedPath:@"image.jpg"];
// 将图片存入该文件夹
[self saveToDocument:self.PlateImageView.image withFilePath:drivingImagePath];
//根据图片名将图片保存到ImageFile文件夹中
-(NSString *)imageSavedPath:(NSString *) imageName
{
//获取Documents文件夹目录
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [path objectAtIndex:0];
//获取文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//指定新建文件夹路径
NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];
//创建ImageFile文件夹
[fileManager createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];
//返回保存图片的路径(图片保存在ImageFile文件夹下)
NSString * imagePath = [imageDocPath stringByAppendingPathComponent:imageName];
// NSString * imagePath = [documentPath stringByAppendingPathComponent:imageName];
return imagePath;
}
//将选取的图片保存到目录文件夹下
-(BOOL)saveToDocument:(UIImage *) image withFilePath:(NSString *) filePath
{
if ((image == nil) || (filePath == nil) || [filePath isEqualToString:@""])
{
return NO;
}
@try {
NSData *imageData = nil;
//获取文件扩展名
NSString *extention = [filePath pathExtension];
if ([extention isEqualToString:@"png"]) {
//返回PNG格式的图片数据
imageData = UIImagePNGRepresentation(image);
}else{
//返回JPG格式的图片数据,第二个参数为压缩质量:0:best 1:lost
imageData = UIImageJPEGRepresentation(image, 0.4);
}
if (imageData == nil || [imageData length] <= 0) {
return NO;
}
//将图片写入指定路径
[imageData writeToFile:filePath atomically:YES];
return YES;
}
@catch (NSException *exception) {
LOG(@"保存图片失败");
}
return NO;
}
// 这里调用接口
DECLARE_WEAK_SELF;//uploadPitureWithLicensePlateNumber
if (self.PlateImage != nil && ![self.PlateFive.text isEqualToString:@""]) {
[SVProgressHUD show];
[[ACUserManager defaultUesrManager]uploadPitureWithLicensePlateNumber:self.PlateFive.text image:self.PlateImage block:^(id info, NSError *error) {
DECLARE_STRONG_SELF;
if (!error) {
if ([info[@"code"] isEqualToString:@"00"]) {
NSLog(@"%@",info);
[SVProgressHUD showSuccessWithStatus:info[@"msg"] duration:1.0];
}else{
// [ACCRToastManager showFailureNotificationWithText:NSLocalizedString(@"失败", nil) subtitleText:NSLocalizedString(info[@"msg"], nil)];
[SVProgressHUD showErrorWithStatus:info[@"msg"] duration:1.0];
NSLog(@"%@",info[@"msg"]);
}
}
[SVProgressHUD dismiss];
}];
// 上传车牌号和图片 这里是接口传给后台
- (void)uploadPitureWithLicensePlateNumber:(NSString *)license image:(UIImage *)image block:(void (^)(id, NSError * ))block{
if (license == nil) {
license = @"";
}
NSDictionary *param=@{@"lpn":license};
NSString *paramStr = [NSString stringWithFormat:@"{\"lpn\":\"%@\"}",license];
NSDictionary *parameters = [self stitchingParameters:param method:@"uploadCarPicture"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:parameters];
[dict setValue:@"memberCar" forKey:@"fileUse"];
[dict setValue:@"PlateNumber" forKey:@"fileName"];
[dict setValue:@"jpg" forKey:@"fileFormat"];
[dict setObject:paramStr forKey:@"param"];
LOG(@"-------------------%@",dict);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置请求格式 manager.requestSerializer = [AFJSONRequestSerializer serializer]; // 设置返回格式 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // formData是遵守了AFMultipartFormData的对象 // kChargingUrl NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"%@",[kUploadCarPictureChargingUrl stringByAppendingString:json]); [manager POST:kUploadCarPictureChargingUrl parameters:dict constructingBodyWithBlock:^(idformData) {
// 将本地的文件上传至服务器
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageFile/image.jpg"];
NSLog(@"%@",path);
NSData *data = [NSData dataWithContentsOfFile:path];
[formData appendPartWithFormData:data name:@"file"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSLog(@"%@", dict);
if (block) {
block(dict, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (block) {
block(nil, error);
}
}];
}