[英]UIImagePickerController fit camera overlay on different screen sizes
我为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;
}
尝试这个....
第1步:
首先为UIImageview
创建IBOutlet
。
-(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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.