我让用户使用 UIImagePickerControllerSourceTypePhotoLibrary 选择图像。
然后我有以下方法获取编辑后的图像,应用叠加层,然后将其传递给我的 PostPictureViewController 进行显示。在显示时,我希望图像保持其原始纵横比并使其适合屏幕。
它在使用 4.3 sdk 的模拟器上运行良好,但是当我把它放在 iPhone 4 视网膜显示屏上时,图像被放大到左上角,只有 1/4 的图像全屏显示,摆脱了原来的纵横比!
我怎样才能缩小它以适应屏幕,就像它在模拟器上工作一样?
我也尝试过完全摆脱覆盖,只返回原始和编辑的图像来显示,但即使这样也不能保持其原始纵横比!!!
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the cropped image from info dictionary
image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
// Combine image with overlay before saving!!
image = [self addOverlayToImage:image];
overlayGraphicView.image = nil;
// Take the picture image to the post picture view controller
postPictureView = [[PostPictureViewController alloc] init:image];
[picker pushViewController:postPictureView animated:YES];
[picker release],picker = nil;
}
这是我的添加叠加图像功能:
- (UIImage*) addOverlayToImage:(UIImage*)originalImage
{
//CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize size = originalImage.size;
if (UIGraphicsBeginImageContextWithOptions != NULL) {
// Use for iPhone 4 Retina
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
} else {
UIGraphicsBeginImageContext(size);
}
UIImage* overlayImage = [UIImage imageNamed:overlayGraphicName];
[originalImage drawAtPoint:CGPointMake(0,0)];
[(UIImage *)overlayImage drawAtPoint:CGPointMake(0,(originalImage.size.height/2 - overlayImage.size.height/2) + 20)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
[finalImage retain];
UIGraphicsEndImageContext();
return finalImage;
}
最后在我的 PostPictureViewController 中,我在 viewdidload 中显示图像:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
// Load the picker image for viewing
UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 480)];
[pickerView setImage:pickerImage];
pickerView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:pickerView];
[pickerView release],pickerView=nil;
}
在这个用于显示图像的 viewdidload 方法中,如果我这样做:
UIImageView *pickerView = [[UIImageView alloc] initWithImage:pickerImage];
那么图像不会在模拟器上垂直居中于屏幕上,而是从 0,0 开始,实际上是使用 CGRectMake 定义帧的宽度和高度,使其在屏幕上居中。
显然,对于 iPhone 4,我也尝试过更改:
UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640, 960)];
但是图像仍然放大到比屏幕更高更大!