来源于
一般提问 分类
UIImagePickerController编辑图片框大小的问题
本人照片的ImageView大小为120*145,而UIImagePickerController的图片编辑框为320*320,如图3白线部分,这样编辑出来的图片大小应该是正方形的,所以放到ImageView里会有问题,请问各位大大,这种情况怎么解决?我不想把ImageView的大小改成方的,能否修改编辑框的大小?





- - (UIImage*)scaleImage:(UIImage*)anImage withEditingInfo:(NSDictionary*)editInfo{
- UIImage *newImage;
- UIImage *originalImage = [editInfo valueForKey:@"UIImagePickerControllerOriginalImage"];
- CGSize originalSize = CGSizeMake(originalImage.size.width, originalImage.size.height);
- CGRect originalFrame;
- originalFrame.origin = CGPointMake(0,0);
- originalFrame.size = originalSize;
- CGRect croppingRect = [[editInfo valueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
- CGSize croppingRectSize = CGSizeMake(croppingRect.size.width, croppingRect.size.height);
- CGSize croppedScaledImageSize = anImage.size;
- float scaledBarClipHeight = 80;
- CGSize scaledImageSize;
- float scale;
- if(!CGSizeEqualToSize(croppedScaledImageSize, originalSize)){
- scale = croppedScaledImageSize.width/croppingRectSize.width;
- float barClipHeight = 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:originalImage to:croppingRect andScaleTo:scaledImageSize];
- }else{
- newImage = originalImage;
- }
- return newImage;
- }
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
- [self dismissModalViewControllerAnimated:YES];
- self.myImageView.userInteractionEnabled=YES;
- CGRect imageFrame = myImageView.frame;
- CGPoint imageCenter = myImageView.center;
- UIImage *croppedImage;
- NSMutableDictionary *imageDescriptor = [editInfo mutableCopy];
- // CGFloat scaleSize = 400.0f;
- CGFloat scaleSize = 640.0f;
- switch ([picker sourceType]) {
- //done
- case UIImagePickerControllerSourceTypePhotoLibrary:
- croppedImage = [self scaleImage:img withEditingInfo:editInfo];
- [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
- break;
- case UIImagePickerControllerSourceTypeCamera: {
- UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
- if (originalOrientation != UIImageOrientationUp) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- CGRect origRect;
- [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
- UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
- CGFloat scale = scaleSize/1600.0f;
- origRect.origin.x *= scale;
- origRect.origin.y *= scale;
- origRect.size.width *= scale;
- origRect.size.height *= scale;
- croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
- [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
- [pool drain];
- }
- else {
- croppedImage = [self scaleImage:img withEditingInfo:editInfo];
- [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
- }
- }
- break;
- case UIImagePickerControllerSourceTypeSavedPhotosAlbum: {
- UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
- if (originalOrientation != UIImageOrientationUp) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- CGRect origRect;
- [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
- UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
- CGFloat scale = scaleSize/640.0f;
- origRect.origin.x *= scale;
- origRect.origin.y *= scale;
- origRect.size.width *= scale;
- origRect.size.height *= scale;
- croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
- [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
- [pool drain];
- }
- else {
- croppedImage = [self scaleImage:img withEditingInfo:editInfo];
- [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
- }
- }
- break;
- default:
- break;
- }
- imageFrame.size = croppedImage.size;
- myImageView.frame = imageFrame;
- myImageView.image = [imageDescriptor objectForKey:@"croppedImage"];
- myImageView.center = imageCenter;
- }
转载自:http://www.cocoachina.com/bbs/read.php?tid=33022