从摄像头/相册获取图片
刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePickerController类来和用户交互。
使用UIImagePickerController和用户交互,我们需要实现2个协议。
View Code
代码如下
#pragma mark 从用户相册获取活动图片
- (void)pickImageFromAlbum
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;//带裁剪框
[self presentModalViewController:imagePicker animated:YES];
}
我们来看看上面的从相册获取图片,我们首先要实例化UIImagePickerController对象,然后设置imagePicker对象为当前对象,设置imagePicker的图片来源为UIImagePickerControllerSourceTypePhotoLibrary,表明当前图片的来源为相册,除此之外还可以设置用户对图片是否可编辑。
View Code
代码如下
#pragma mark 从摄像头获取活动图片
- (void)pickImageFromCamera
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];
}
以上是从摄像头获取图片,和从相册获取图片只是图片来源的设置不一样,摄像头图片的来源为UIImagePickerControllerSourceTypeCamera。
在和用户交互之后,用户选择好图片后,会回调选择结束的方法。
View Code
代码如下
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
theImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(120.0, 120.0)];
UIImage *midImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
UIImage *bigImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 440.0)];
[theImage retain];
[self saveImage:theImage WithName:@"salesImageSmall.jpg"];
[self saveImage:midImage WithName:@"salesImageMid.jpg"];
[self saveImage:bigImage WithName:@"salesImageBig.jpg"];
[self dismissModalViewControllerAnimated:YES];
[self refreshData];
[picker release];
}
在回调结束的方法中,我们对图片进行了大小的处理,为图片的上传做准备。
缩放图片
缩放图片比较简单,就直接放上代码,让大家参考一下。
View Code
代码如下
//压缩图片 带裁剪框
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
iOS 压缩图片 如下
//压缩图片尺寸 不带裁剪框
float scales = image.size.height / image.size.width;
UIImage *normalImg;
NSData *newData;
//(一)
/*
如果需要改动被压大小,调整scale,而不是kk或aa
*/
if (image.size.width > 1000 || image.size.height > 1000) {//这里的1000就是scale,所有的都要随着改变
if (scales > 1) {
CGSize newSize = CGSizeMake(1000 / scales, 1000);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}else {
CGSize newSize = CGSizeMake(1000 ,1000 * scales);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}
}
else {
normalImg=image;
}
//(二)
CGSize newSize = CGSizeMake(normalImg.size.width, normalImg.size.height);
UIGraphicsBeginImageContext(newSize);
[normalImg drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
//图片压缩系数
float kk = 1.0f;
//图片压缩系数变化步长(可变)
float aa = 0.1f;
//压缩后的大小
int mm;
mm=(int)UIImageJPEGRepresentation(newImage, kk).length;
while (mm/1024 > 100) {
if (kk > aa+aa/10) {
kk -= aa;
mm = (int)UIImageJPEGRepresentation(newImage, kk).length;
}else{
aa /= 10;
}
}
newData = UIImageJPEGRepresentation(newImage, kk);//最后压缩结果
NSLog(@"11111------------===%ld",(long)newData.length/1024);
if (newData.length/1024 > 100) {
return nil;
}else{
UIImage *image = [UIImage imageWithData:newData];
return image;
}
}