对图片进行压缩,检查内存,保持在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];
}