如何通过添加叠加层来拍摄照片

3

我尝试在拍照时添加叠加层。我成功地在预览中添加了叠加层,但一旦我点击拍照按钮,叠加层视图(大约40像素)将翻转,并且我还尝试将图像保存在相册中,但保存的图像不包含叠加层图像。

我附上了以下内容,如果我错了,请指导我?

//In ViewDidLoad//
OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

[picker setDelegate:self];  
picker.showsCameraControls = YES;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;



picker.wantsFullScreenLayout = YES;


picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 1, 1.24299);


picker.cameraOverlayView = overlay;

[self presentModalViewController:picker animated:YES];  
[picker release];




- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);


}



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

// Unable to save the image  
if (error)
    alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                       message:@"Unable to save image to Photo Album." 
                                      delegate:self cancelButtonTitle:@"Ok" 
                             otherButtonTitles:nil];
else // All is well
    alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                       message:@"Image saved to Photo Album." 
                                      delegate:self cancelButtonTitle:@"Ok" 
                             otherButtonTitles:nil];


[alert show];
alert.delegate = self;
[alert release];
}


//In OverlayView.m file

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
    // Clear the background of the overlay:
    self.opaque = NO;
    self.backgroundColor = [UIColor clearColor];

    // Load the image to show in the overlay:
    UIImage *overlayGraphic = [UIImage imageNamed:@"img3.png"];
    UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
    overlayGraphicView.frame = CGRectMake(0, 50, 320, 430);
    [self addSubview:overlayGraphicView];
    [overlayGraphicView release];

}
return self;
}

谢谢您的预先帮助


UIImagePickerController即使添加了覆盖视图,也能提供清晰的图像。 - Paresh Navadiya
您可以使用UIGraphicsImageContext的drawInRect方法创建图像并添加覆盖视图。 - Paresh Navadiya
请问您能提供任何示例代码吗? - thavasidurai
1个回答

5

拍照后创建图像,如下所示:

UIImage *capturedImage = [UIImage imageNamed:@"bottom.png"]; //Captured image
UIImage *overlayImage  = [UIImage imageNamed:@"top.png"]; //foverlayImage image

CGSize newSize = capturedImage.size
UIGraphicsBeginImageContext( newSize );

[capturedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

// Apply supplied opacity if applicable
[overlayImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.7];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

我可以将这段代码添加到以下方法中吗?
  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- thavasidurai
根据您的要求设置覆盖层的框架。 - Paresh Navadiya
是的,它可以工作,但是叠加的图像与相机拍摄的图像重叠时是透明的。 - thavasidurai
如果capturedImage和overlayImage具有不同的大小或宽高比,这是否会导致问题?这是我目前尝试做类似操作面临的问题。capturedImage来自相机,因此其宽高比为4:3,而overlayImage的宽高比不同,因此overlyImage会变形(宽高比未保留)。 - Peter Jacobs
@PeterJacobs 我也遇到了相同的问题,即纵横比(我的叠加图像大小在显示图像时发生变化)。如果有人知道,请帮帮我... - Divyesh Dobariya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接