ios拍照过程

粗谈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.根据接口内容,调用相关接口把图片上传到服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wxf_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值