//原始图
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSData *data;
CGFloat fixelW = image.size.width;
CGFloat fixelH = image.size.height;
CGFloat maxValue = fixelW;
if( fixelH > maxValue){
maxValue = fixelH;
}
if( maxValue > 800){
CGFloat scaleSize = 800/maxValue;
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(image, 0.5);
}else{
data = UIImageJPEGRepresentation(image,1);
}
上面是修改图大小,按图片品质生成UIImage
下面是选取中图片,修改大小,保存本地,生成base64,
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSString *type = [info objectForKey:@"UIImagePickerControllerMediaType"];
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
//原始图
// UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSData *data;
CGFloat fixelW = image.size.width;
CGFloat fixelH = image.size.height;
CGFloat maxValue = fixelW;
if( fixelH > maxValue){
maxValue = fixelH;
}
if( maxValue > 800){
CGFloat scaleSize = 800/maxValue;
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(image, 0.5);
}else{
data = UIImageJPEGRepresentation(image,1);
}
// NSData *data;
// if (UIImagePNGRepresentation(image) == nil)
// {
// data = UIImageJPEGRepresentation(image, 0.5);
// }
// else
// {
// data = UIImagePNGRepresentation(image);
// }
//图片保存路径
//这里将图片放在沙盒的documents/image文件夹中
NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *imgPath = [documentsPath stringByAppendingPathComponent:@"image"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//生成唯一字符串
NSString *uuid = [[NSUUID UUID]UUIDString];
//生成文件名
NSString *fileName = [NSString stringWithFormat:@"%@.png",uuid];
//把刚刚由图片转成的data对象拷贝至沙盒中 并保存为xxxxx-xxxx-xxx...xxx.png
/******保存之前最好先清空下,不然占用磁盘越来越大********/
[fileManager removeItemAtPath:imgPath error:nil];
/*************************************************/
[fileManager createDirectoryAtPath:imgPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[imgPath stringByAppendingPathComponent:fileName] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@",[imgPath stringByAppendingPathComponent:fileName]];
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^(void){}];
NSString* outBase64Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
// base64EncodedStringWithOptions()
ImagePicker::getInstance()->outBase64 = [outBase64Str UTF8String];
//NSLog(@"打印字符串:%p%@",outBase64Str,ImagePicker::getInstance()->outBase64);
// char **out;
// base64Encode(*data, data.length, **out);
//通知ImagePicker完成取图
std::string strFilePath = [filePath UTF8String];
// ImagePicker::getInstance()->m_callback(strFilePath);
// NSString *callFun =@"onImageSaved(";
NSString *callFun =@"onImageSaved('";
// NSString *s2 = @"";
NSString *s3 = @"')";
NSString *s4 =[NSString stringWithFormat:@"%@%@%@", callFun,filePath,s3];
// NSLog([s4 UTF8String]);
se::ScriptEngine::getInstance()->evalString([s4 UTF8String]);
}
}