首先是调用相机和本地上传图片功能,这里要判断一下设备是否有拍照功能。如果是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];
}
+(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(0, 0,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;
}