如何设置UIImageView的正确大小

时间:2013-09-20 15:49:32

标签: ios objective-c xcode uiimageview uiimagepickercontroller

我已经从developer.apple.com下载了示例代码“使用UIImagePickerController选择图片和拍照”,基本上我把它原样保留了下来。我添加了功能,如果我拍了一些照片,我再次在imagePickerController视图中拍摄另一张照片,它会覆盖之前拍摄的照片。还有滑块,我可以改变叠加层的不透明度,效果很好。

然而,之前拍摄的照片未按比例缩放以适合视图,而只能看到原始分辨率的部分照片。那么如何将overlayImage设置为合适的大小?我尝试过使用storyboards / nib文件/以编程方式添加它但没有成功。

@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *overlayImage;
@property (nonatomic) IBOutlet UIView *overlayView;

以下是方法:

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
if (self.imageView.isAnimating)
{
    [self.imageView stopAnimating];
}

if (self.capturedImages.count > 0)
{
    //[self.capturedImages removeAllObjects];
}

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = NO;

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    self.overlayView.frame = imagePickerController.cameraOverlayView.frame;
    imagePickerController.cameraOverlayView = self.overlayView;
    self.overlayView = nil;
    self.overlayImage = self.imageView;
    self.overlayImage.alpha = 0.5;

    UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(60, 80, 200, 30)];
    [slider setMaximumValue:1.0];
    [slider setMinimumValue:0.0];
    [slider setValue:0.5];
    [slider addTarget:self action:@selector(sliderChanged:)  forControlEvents:UIControlEventValueChanged];

    if (self.capturedImages.count > 0)
    {
        [imagePickerController.cameraOverlayView addSubview:self.overlayImage];
        [imagePickerController.cameraOverlayView addSubview:slider];
    }
}

self.imagePickerController = imagePickerController;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:0)

在您的代码示例中,我看不到在任何地方设置self.imageView.frame。如果我理解这个问题,那就是问题所在。

答案 1 :(得分:0)

解决!简单:

self.overlayImage.image = self.imageView.image;

而不是:

self.overlayImage = self.imageView;