修改UIImage 宽高并压缩

//原始图
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]);        
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游鱼_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值