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;
}
本文介绍了一种处理图片编辑信息的方法,通过使用编辑信息来获取原始图片的裁剪区域。这种方法可以应用于从不同来源(如相册或相机)选择的图片,并能够处理图片的方向问题。文中提供了一个具体的实现示例,包括如何根据编辑信息调整裁剪区域并缩放图片。
1873

被折叠的 条评论
为什么被折叠?



