粗谈iOS简单的拍照上传过程
1.声明几个按钮对象
@property (nonatomic,retain) UIButton *upload1;
2.创建按钮,触发拍照动作
_upload1 = [UIButton buttonWithType:UIButtonTypeSystem];
_upload1.frame = CGRectMake(100, 300, 40, 30);
_upload1.tag = 10;
[_upload1 setBackgroundImage:[UIImage imageNamed:@”take_picture”] forState:UIControlStateNormal];
[_upload1 addTarget:self action:@selector(uploadPhoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_upload1];
3.相机调用
(void)uploadPhoto:(UIButton *)btn
{
_upTag = btn.tag;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
//摄像头
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES; //是否可编辑
self.imgPicker = picker;[self presentModalViewController:picker animated:YES];
}
else{
//如果没有提示用户
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Error” message:@”你没有摄像头” delegate:nil cancelButtonTitle:@”Drat!” otherButtonTitles:nil];
[alert show];
}
}
4.获取拍照的图片并保存到本地(UIimagePickerControllerDelegate )
(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info
{
//获取拍照出来的图像
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];if (_upTag == 10)
{
// 保存图片至本地
[self saveImage:image withName:@”image1.jpg”];
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@”Documents”] stringByAppendingPathComponent:@”image1.jpg”];
[_upload1 setBackgroundImage:[UIImage imageWithContentsOfFile:fullPath] forState:UIControlStateNormal];
[self imageToBase64WithPath:fullPath btnTag:_upTag];
}
[self dismissModalViewControllerAnimated:YES];
}
//将文件存至沙盒- (void)saveImage:(UIImage )currentImage withName:(NSString )imageName
{
NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.01f);
// 获取沙盒目录
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@”Documents”] stringByAppendingPathComponent:imageName];
// 将图片写入文件
[imageData writeToFile:fullPath atomically:YES];
}
//图片转base64 - (void)imageToBase64WithPath:(NSString *)path btnTag:(int)tag
{
UIImage *image1 = [UIImage imageWithContentsOfFile:path];
NSData *data = UIImageJPEGRepresentation(image1, 0.5f);
if (10 == tag) {
_image1Base64String = [data base64Encoding];
}
}
//判断png还是jpg图片,有alpha是png图片,否则jpg图片 - (BOOL) imageHasAlpha: (UIImage *) image
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
//image转base64 - (NSString ) image2Base64: (UIImage ) image
{
NSData *imageData = nil;
imageData = UIImageJPEGRepresentation(image, 0.01f);
return [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
/根据图片的类型进行相应的压缩(可要可不要)/
// NSString *mineType = nil;
// if ([self imageHasAlpha: image]) {
// UIImage *imageNew=image;
// CGSize imagesize = imageNew.size;
// CGSize sizeTemp = imageNew.size;
// imagesize.height =sizeTemp.height/10;
// imagesize.width =sizeTemp.width/10;//
image1Type = @”png”;
//
// } else {
// imageData = UIImageJPEGRepresentation(image, 0.01f);
image1Type = @”jpg”;
// }
// return [NSString stringWithFormat:@”%@”,[imageData base64EncodedStringWithOptions: 0]];//NSDATA—>BASE64
}
图片处理
//对图片尺寸进行压缩–
-(UIImage*)imageWithImage:(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;
}
//把图片剪裁压缩之后转换成base64string类型
- (NSString )image2Base64WithImage:(UIImage)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *dataImg = UIImageJPEGRepresentation(newImage, 0.001);
NSString *strImg = [dataImg base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
return strImg;
}
5.根据接口内容,调用相关接口把图片上传到服务器