UIImagePickerController适合不同屏幕尺寸的相机覆盖

时间:2014-05-02 04:17:52

标签: ios objective-c uiimagepickercontroller

我为UIImagePickerController制作了自定义叠加层,它在iPhone 5上运行效果很好,但在4或4S上进行测试时叠加层太大了。

如何在两种尺寸的屏幕上正确匹配叠加?

以下是我的相机覆盖的init方法。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];

        // load an image to show in the overlay
        UIImage *constraints = [UIImage imageNamed:@"camera_overlay"];
        UIImageView *constraintView = [[UIImageView alloc]initWithImage:constraints];
        CGRect screenRect = [[UIScreen mainScreen]bounds];
        constraintView.frame = CGRectMake(27, 10, screenRect.size.width - 50, screenRect.size.height - 80);
        [self addSubview:constraintView];

        _shootButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *shootImage = [UIImage imageNamed:@"cameraOverlayShootButton"];
        UIImage *shootImageDown = [UIImage imageNamed:@"cameraOverlayShootDown"];

        [_shootButton setBackgroundImage:shootImage forState:UIControlStateNormal];
        [_shootButton setBackgroundImage:shootImageDown forState:UIControlStateHighlighted];
        _shootButton.frame = CGRectMake(115, 465, 85, 85);
        [self addSubview:_shootButton];

        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *cancelImage = [UIImage imageNamed:@"cancelButton"];

        [_cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal];
        _cancelButton.frame = CGRectMake(265, 505, 25, 25);
        [self addSubview:_cancelButton];

        _flashButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *flashImage = [UIImage imageNamed:@"autoFlash"];

        [_flashButton setBackgroundImage:flashImage forState:UIControlStateNormal];
        _flashButton.frame = CGRectMake(8, 10, 40, 40);
        [self addSubview:_flashButton];


    }
    return self;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个......

第1步:

首先为IBOutlet创建UIImageview

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];


if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    OriginalImage=info[UIImagePickerControllerOriginalImage];
    image = info[UIImagePickerControllerOriginalImage];
//----------------------------------------
     imageview.image = image; //------------- additional method for custom image size
     [self resizeImage];


//-----------------------------------------
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil);

}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}

}

第2步:添加此方法以在所有设备中获取通用尺寸的图像

-(void)resizeImage
{
    UIImage *resizeImage = imageview.image;

    float width = 320;
    float height = 320;

    CGSize newSize = CGSizeMake(320,320);
    UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
    CGRect rect = CGRectMake(0, 0, width, height);

    float widthRatio = resizeImage.size.width / width;
    float heightRatio = resizeImage.size.height / height;
    float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

    width = resizeImage.size.width / divisor;
    height = resizeImage.size.height / divisor;

    rect.size.width  = width;
    rect.size.height = height;

    //indent in case of width or height difference
    float offset = (width - height) / 2;
    if (offset > 0) {
        rect.origin.y = offset;
    }
    else {
        rect.origin.x = -offset;
    }

    [resizeImage drawInRect: rect];

    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

imageview.image = smallImage;
imageview.contentMode = UIViewContentModeScaleAspectFit;

}