对图片进行压缩,检查内存,保持在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];
}
本文介绍了一种在iOS应用中实现图片压缩的方法,并确保压缩后的图片文件大小不超过40K。文章详细展示了如何使用UIImagePickerController选择图片,通过自定义方法调整图片尺寸并进行压缩,以及如何将压缩后的图片保存到相册及模拟器桌面。
9143

被折叠的 条评论
为什么被折叠?



