我从 developer.apple.com 下载了示例代码“使用 UIImagePickerController 选择图片并拍照”,基本上我保持原样。我已经添加了功能,如果我拍摄了一些照片,并且我再次在 imagePickerController 视图中拍摄另一张照片,它会与之前拍摄的照片重叠。还有滑块,我可以更改叠加层的不透明度,效果很好。
但是,之前拍摄的照片并未缩放以适合视图,而是只能看到原始分辨率的一部分。那么如何将 overlayImage 设置为适当的大小?我试过使用故事板/笔尖文件/以编程方式添加它,但没有成功。
@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];
}