简体   繁体   中英

How to set proper size of UIImageView

I've downloaded sample code "Using UIImagePickerController to Select Pictures and Take Photos" from developer.apple.com and basically I left it as it was. I've added functionality, that if I have taken some picture, and I'm again in imagePickerController view to take another picture, it's overlaid with that previously taken picture. There is also slider and I can change opacity of overlay, works great.

However that previously taken picture is not scaled to fit the view, instead only part of it in original resolution can be seen. So how can I set overlayImage to proper size? I've tried using storyboards/nib files/add it programmatically but no success.

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

Here's the method:

- (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];
}

In your code sample I can't see setting self.imageView.frame anywhere. If I understand the question right, this is the problem.

Solved! with simple:

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

instead of:

self.overlayImage = self.imageView;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM