客户端拍照处理最终版

首先是调用相机和本地上传图片功能,这里要判断一下设备是否有拍照功能。如果是ipad或者itouch就不能使用此功能了。
调用之后进入下一个页面。

#pragma mark Photo

- (void)selectPhotoBySnaping{//调相机

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

saveImage = YES;

UIImagePickerController *imagePicker= [[UIImagePickerController alloc] init];

imagePicker.delegate self;

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentModalViewController:imagePicker animated:YES];

[imagePicker release];

}

}


- (void)selectPhotoFromLibrary{//调出相册

UIImagePickerController *imagePicker= [[UIImagePickerController alloc] init];

imagePicker.delegate self;

imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//UIImagePickerControllerSourceTypePhotoLibrary;

[self presentModalViewController:imagePicker animated:YES];

[imagePicker release];

}


#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info{

//NSLog(@"info %@",info);

UIImage *reviewImage= [info objectForKey:UIImagePickerControllerOriginalImage];

if (YES == saveImage){

saveImage = NO;

UIImageWriteToSavedPhotosAlbum(reviewImage, nil, nil, nil);

}

[self dismissModalViewControllerAnimated:NO];

[self ImageTextReview:reviewImage];

[reviewImage release];

}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [self dismissModalViewControllerAnimated:NO];

}



进入下一个页面之后,图片需要上传到服务器上,但是iphone的图片方向和其它的有偏差,所以需要更正图片方向。

+(UIImage *)rotateImage:(UIImage *)aImage

{

CGImageRef imgRef = aImage.CGImage;

UIImageOrientation orient= aImage.imageOrientation;

UIImageOrientation newOrient= UIImageOrientationUp;

switch (orient) {

case 3://竖拍 home键在下

newOrient= UIImageOrientationRight;

break;

case 2://倒拍 home键在上

newOrient= UIImageOrientationLeft;

break;

case 0://左拍 home键在右

newOrient= UIImageOrientationUp;

break;

case 1://右拍 home键在左

newOrient= UIImageOrientationDown;

break;

default:

newOrient= UIImageOrientationRight;

break;

}

CGFloat width= CGImageGetWidth(imgRef);

CGFloat height= CGImageGetHeight(imgRef);

CGFloat ratio= 0;

if ((width> 1024)|| (height > 1024)){

if (width >= height) {

ratio = 1024/width;

}

else {

ratio = 1024/height;

}

width *= ratio;

height *= ratio;

}

CGAffineTransform transform= CGAffineTransformIdentity;

CGRect bounds= CGRectMake(00,width, height);

CGFloat scaleRatio= 1;

CGFloat boundHeight;

switch(newOrient)

{

case UIImageOrientationUp

transform= CGAffineTransformIdentity;

break;

case UIImageOrientationDown

transform= CGAffineTransformMakeTranslation(width,height);

transform = CGAffineTransformRotate(transform, M_PI);

break;

case UIImageOrientationLeft

 

boundHeight = bounds.size.height;

bounds.size.height =bounds.size.width;

bounds.size.width =boundHeight;

transform= CGAffineTransformMakeTranslation(0.0,width);

transform = CGAffineTransformRotate(transform, 3.0 M_PI 2.0);

break;

case UIImageOrientationRight

boundHeight = bounds.size.height;

bounds.size.height =bounds.size.width;

bounds.size.width =boundHeight;

transform= CGAffineTransformMakeTranslation(height, 0.0);

transform = CGAffineTransformRotate(transform, M_PI 2.0);

break;

default:

[NSException raise:NSInternalInconsistencyException format:@"Invalidimage orientation"];

}

UIGraphicsBeginImageContext(bounds.size);

CGContextRef context= UIGraphicsGetCurrentContext();

if (newOrient== UIImageOrientationRight ||newOrient== UIImageOrientationLeft

{

CGContextScaleCTM(context, -scaleRatio, scaleRatio);

CGContextTranslateCTM(context,-height, 0);

}

else 

{

CGContextScaleCTM(context, scaleRatio, -scaleRatio);

CGContextTranslateCTM(context, 0,-height);

}

CGContextConcatCTM(context, transform);

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0,width, height), imgRef);

UIImage *imageCopy= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return imageCopy;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值