iOS 图片压缩处理、模拟器环境检测

对图片进行压缩,检查内存,保持在40K以下,保存在相册,若是模拟器,就再保存一份在mac桌面,动态检测mac桌面地址。

@interface dhViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property(strong,nonatomic)UIImage* m_selectImage;
@property (weak, nonatomic) IBOutlet UILabel *pre;
@property (weak, nonatomic) IBOutlet UILabel *final_date;
@property (weak, nonatomic) IBOutlet UIButton *TakePhotoBtn;
@property(strong,nonatomic)UIImagePickerController* ipc;
@end

@implementation dhViewController
@synthesize iv;
@synthesize m_selectImage;
@synthesize TakePhotoBtn;
@synthesize pre;
@synthesize final_date;
@synthesize ipc;

- (IBAction)clickBtn:(id)sender {

    ipc=[[UIImagePickerController alloc]init];
    ipc.delegate=self;
    [ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:ipc animated:YES completion:nil];

}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//    NSLog(@"%@",info);
    UIImage* imageNew=[info valueForKeyPath:@"UIImagePickerControllerOriginalImage"];
//    //取得的对象虽然是同一张图片,但对象不一样,所以不能这样判断image,应该判断Url
    NSLog(@"image:%@",imageNew);
    //设置image的尺寸
    CGSize imagesize = imageNew.size;
    imagesize.height =self.view.bounds.size.height;
    imagesize.width =self.view.bounds.size.width;
    
    //对图片大小进行压缩--
    imageNew = [self imageRarZip:imageNew scaledToSize:imagesize];
    NSData *imageData = UIImageJPEGRepresentation(imageNew,1.0);
    NSLog(@"操作前数据大小:%lu",(unsigned long)imageData.length);
    pre.text = [NSString stringWithFormat:@"操作前:%lu bit",(unsigned long)imageData.length];
    //大小控制
    float m = 0.01;
    float n = 1.0;
    while ((unsigned long)imageData.length>1024*40) {
        n = n - m;
         imageData = UIImageJPEGRepresentation(imageNew,n);
        NSLog(@"数据大小:%lu",(unsigned long)imageData.length);
    }
    NSLog(@"数据最终大小:%lu",(unsigned long)imageData.length);
    final_date.text = [NSString stringWithFormat:@"操作后:%lu bit",(unsigned long)imageData.length];
    
        m_selectImage = [UIImage imageWithData:imageData];
        
        NSLog(@"m_selectImage:%@",m_selectImage);
        NSFileManager *fileManager = [NSFileManager defaultManager];
//    如果不支持相机功能,那么就是模拟器,那么就可以保存在本地
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
       
        NSString* bundel=[[NSBundle mainBundle] resourcePath];
        NSString* deskTopLocation=[[bundel substringToIndex:[bundel rangeOfString:@"Library"].location] stringByAppendingFormat:@"Desktop"];
        NSLog(@"%@", deskTopLocation);
        
       
         UIAlertView * pg = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"当前运行环境是模拟器,图片压缩除了保存相册,还保存在%@",deskTopLocation] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [pg show];
        NSString *filePath = deskTopLocation;         //将图片存储到本地桌面
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:imageData attributes:nil];
    }

        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:imageData], nil, nil, nil);//存到相册
        iv.image = m_selectImage;
        [picker dismissViewControllerAnimated:YES completion:nil];
}
//修改图片尺寸
-(UIImage*)imageRarZip:(UIImage*)image scaledToSize:(CGSize)n_size{
    UIGraphicsBeginImageContext(n_size);
    [image drawInRect:CGRectMake(0,0,n_size.width,n_size.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值