This includes most of what you need, and takes care of all thecamera orientation issues. I've added the following which will takein the editing info and use it to get the original cropping rectwith this addition:
- (UIImage*)scaleImage:(UIImage*)anImagewithEditingInfo:(NSDictionary*)editInfo{
UIImage*newImage;
UIImage*originalImage = [editInfovalueForKey:@"UIImagePickerControllerOriginalImage"];
CGSizeoriginalSize = CGSizeMake(originalImage.size.width,originalImage.size.height);
CGRectoriginalFrame;
originalFrame.origin= CGPointMake(0,0);
originalFrame.size= originalSize;
CGRectcroppingRect = [[editInfovalueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
CGSizecroppingRectSize = CGSizeMake(croppingRect.size.width,croppingRect.size.height);
CGSizecroppedScaledImageSize = anImage.size;
floatscaledBarClipHeight = 80;
CGSizescaledImageSize;
floatscale;
if(!CGSizeEqualToSize(croppedScaledImageSize,originalSize)){
scale= croppedScaledImageSize.width/croppingRectSize.width;
floatbarClipHeight = scaledBarClipHeight/scale;
croppingRect.origin.y-= barClipHeight;
croppingRect.size.height+= (2*barClipHeight);
if(croppingRect.origin.y<=0){
croppingRect.size.height+= croppingRect.origin.y;
croppingRect.origin.y=0;
}
if(croppingRect.size.height> (originalSize.height -croppingRect.origin.y)){
croppingRect.size.height= (originalSize.height - croppingRect.origin.y);
}
scaledImageSize= croppingRect.size;
scaledImageSize.width*= scale;
scaledImageSize.height*= scale;
newImage= [self cropImage:originalImageto:croppingRect andScaleTo:scaledImageSize];
}else{
newImage= originalImage;
}
returnnewImage;
}
I updated the call back method from the dev forums post to thefollowing:
复制代码
- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary*)editInfo {
[selfdismissModalViewControllerAnimated:YES];
self.myImageView.userInteractionEnabled=YES;
CGRectimageFrame = myImageView.frame;
CGPointimageCenter = myImageView.center;
UIImage*croppedImage;
NSMutableDictionary*imageDescriptor = [editInfo mutableCopy];
//CGFloat scaleSize = 400.0f;
CGFloatscaleSize = 640.0f;
switch([picker sourceType]) {
//done
caseUIImagePickerControllerSourceTypePhotoLibrary:
croppedImage= [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
break;
caseUIImagePickerControllerSourceTypeCamera: {
UIImageOrientationoriginalOrientation = [[editInfoobjectForKey:UIImagePickerControllerOriginalImage]imageOrientation];
if(originalOrientation != UIImageOrientationUp) {
NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
CGRectorigRect;
[[editInfoobjectForKey:UIImagePickerControllerCropRect]getValue:&origRect];
UIImage*rotatedImage = straightenAndScaleImage([editInfoobjectForKey:UIImagePickerControllerOriginalImage],scaleSize);
CGFloatscale = scaleSize/1600.0f;
origRect.origin.x*= scale;
origRect.origin.y*= scale;
origRect.size.width*= scale;
origRect.size.height*= scale;
croppedImage= [self cropImage:rotatedImage to:origRectandScaleTo:CGSizeMake(320, 480)];
[imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
[pooldrain];
}
else{
croppedImage= [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
}
}
break;
caseUIImagePickerControllerSourceTypeSavedPhotosAlbum: {
UIImageOrientationoriginalOrientation = [[editInfoobjectForKey:UIImagePickerControllerOriginalImage]imageOrientation];
if(originalOrientation != UIImageOrientationUp) {
NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
CGRectorigRect;
[[editInfoobjectForKey:UIImagePickerControllerCropRect]getValue:&origRect];
UIImage*rotatedImage = straightenAndScaleImage([editInfoobjectForKey:UIImagePickerControllerOriginalImage],scaleSize);
CGFloatscale = scaleSize/640.0f;
origRect.origin.x*= scale;
origRect.origin.y*= scale;
origRect.size.width*= scale;
origRect.size.height*= scale;
croppedImage= [self cropImage:rotatedImage to:origRectandScaleTo:CGSizeMake(320, 480)];
[imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
[pooldrain];
}
else{
croppedImage= [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
}
}
break;
default:
break;
}
imageFrame.size= croppedImage.size;
myImageView.frame= imageFrame;
myImageView.image= [imageDescriptor objectForKey:@"croppedImage"];
myImageView.center= imageCenter;
}