编辑时如何将图像叠加到 UIImagePickerControllerSourceTypePhotoLibrary 中?

发布于 2024-11-09 11:25:59 字数 1836 浏览 0 评论 0原文

我尝试到处寻找这个问题的答案,但找不到任何有用的信息。

基本上,我希望用户从相册中选择一张图片,然后让应用程序以编辑/裁剪模式显示图片。

在此编辑屏幕上,我想显示一个覆盖屏幕,供用户用来对齐其图像。

然后,他单击“保存”按钮,通过合并图像来保存编辑后的图片和覆盖图像。

我唯一找不到如何做的就是在编辑屏幕中添加叠加图像!

在以下情况下我可以很容易地做到这一点:

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

但是当:

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

我无法使用:

imagePicker.cameraOverlayView = overlayGraphicView;

这就是我的问题。

另外,如果我添加 UIimage 作为子视图而不是此叠加层,即使用户从相册中选择他的图像,它也会保留在屏幕上。我只希望在选择图像进行编辑之后以及编辑完成后将其保存到相册之前显示叠加层。

感谢您的帮助。

方法如下:

- (IBAction)pickPhotoButton:(id)sender {
    NSLog(@"Pick a Current Photo Button Pressed...");

    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    imagePicker.delegate = self;

    // Allow editing of image ?
    imagePicker.allowsEditing = YES;

    // Show image picker
    [self presentModalViewController:imagePicker animated:YES]; 
}

这是我保存到相册的方法:

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

    // Combine image with overlay before saving!!
    image = [self addOverlayToImage:image];

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

    [picker release];
}

I've tried looking everywhere for an answer to this but can not find any useful info.

Basically, I want the user to pick a picture from the album, and then have the app display the picture in the edit/crop mode.

On this edit screen I want to display an overlay screen for the user to use to align his image.

He then clicks the save button which saves the edited picture with the overlay image by just merging the images.

The only thing I can not find how to do is add the overlay image in the edit screen!

I can do this quite easily when:

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

But when:

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

I can not use:

imagePicker.cameraOverlayView = overlayGraphicView;

and that is my issue.

Also if I add an UIimage as a subview instead then this overlay, it stays on the screen even when the user is choosing his image from the album. I only want the overlay to appear after the image has been chosen for editing and before it has been saved to the album after the editing has finished.

Thanks for your help.

Heres the method:

- (IBAction)pickPhotoButton:(id)sender {
    NSLog(@"Pick a Current Photo Button Pressed...");

    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    imagePicker.delegate = self;

    // Allow editing of image ?
    imagePicker.allowsEditing = YES;

    // Show image picker
    [self presentModalViewController:imagePicker animated:YES]; 
}

and heres my save to album method:

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

    // Combine image with overlay before saving!!
    image = [self addOverlayToImage:image];

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

    [picker release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-11-16 11:25:59

您可以成为 UIImagePickerController 对象的委托并敏锐地观察 UINavigationControllerDelegate 方法 - navigationController:didShowViewController:animated:navigationController:willShowViewController:animated:。您可以知道第二个视图控制器何时启动,然后将覆盖层添加到视图控制器的视图中。示例用法如下所示 –

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ( [navigationController.viewControllers count] == 2 ) {
        overlayView.center = CGPointMake(135, 135);
        overlayView.bounds = CGRectZero;

        [viewController.view addSubview:overlayView];

        [UIView beginAnimations:@"animateTableView" context:nil];
        [UIView setAnimationDuration:0.4];
        [overlayView setFrame:CGRectMake( 50, 50, 220, 220)];
        [UIView commitAnimations];
    }
}

You can become UIImagePickerController object's delegate and keenly observe the UINavigationControllerDelegate methods - navigationController:didShowViewController:animated: and navigationController:willShowViewController:animated:. You can know when the second view controller is coming on and later add your overlay to the view controller's view. Sample usage is shown below –

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ( [navigationController.viewControllers count] == 2 ) {
        overlayView.center = CGPointMake(135, 135);
        overlayView.bounds = CGRectZero;

        [viewController.view addSubview:overlayView];

        [UIView beginAnimations:@"animateTableView" context:nil];
        [UIView setAnimationDuration:0.4];
        [overlayView setFrame:CGRectMake( 50, 50, 220, 220)];
        [UIView commitAnimations];
    }
}
美煞众生 2024-11-16 11:25:59

您可以使用以下自定义方法将叠加层添加到 Imagepickercontroller

在您的classname .h 文件中定义

@property (nonatomic, retain) UIImagePickerController*      m_Pickercontroller;
@property(nonatomic,retain) UIView *cameraOverlayView;


 In yourclassname.m file

-(void)Loadcaptureview
{
   //Allocating Image Picker Controller 
    m_Pickercontroller=[[UIImagePickerController alloc] init ];
    m_Pickercontroller.delegate=self;
    m_Pickercontroller.allowsEditing=YES;
    m_Pickercontroller.sourceType=UIImagePickerControllerSourceTypeCamera;
    m_Pickercontroller.view.frame=CGRectMake(0, 0, 320, 300); 

    UIButton*Clickbutton=[[UIButton alloc] initWithFrame:CGRectMake(50,200, 60, 60)];
    [Clickbutton setImage:[UIImage imageNamed:@"red-circle-button.png"] forState:UIControlStateNormal];
    [Clickbutton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

    image1=[[UIImageView alloc] initWithFrame:CGRectMake(50,400, 60, 60)];
    image1.image=[UIImage imageNamed:@"red-circle-button.png"];

    [m_Pickercontroller.cameraOverlayView addSubview:Clickbutton];
    [m_Pickercontroller.cameraOverlayView addSubview:image1];
    [m_Pickercontroller.cameraOverlayView bringSubviewToFront:image1];

    //Hiding the Camera Control
    m_Pickercontroller.showsCameraControls=NO;

    //Presenting the imagepicker Controller to Present Modal view Controller
    [self.navigationController presentModalViewController:m_Pickercontroller animated:YES];
    }

  // Calling the Function

  - (void)viewDidLoad
  {

   [self Loadcaptureview];

   }

You can use the following customised method for adding overlays to the Imagepickercontroller

In yourclassname .h file define

@property (nonatomic, retain) UIImagePickerController*      m_Pickercontroller;
@property(nonatomic,retain) UIView *cameraOverlayView;


 In yourclassname.m file

-(void)Loadcaptureview
{
   //Allocating Image Picker Controller 
    m_Pickercontroller=[[UIImagePickerController alloc] init ];
    m_Pickercontroller.delegate=self;
    m_Pickercontroller.allowsEditing=YES;
    m_Pickercontroller.sourceType=UIImagePickerControllerSourceTypeCamera;
    m_Pickercontroller.view.frame=CGRectMake(0, 0, 320, 300); 

    UIButton*Clickbutton=[[UIButton alloc] initWithFrame:CGRectMake(50,200, 60, 60)];
    [Clickbutton setImage:[UIImage imageNamed:@"red-circle-button.png"] forState:UIControlStateNormal];
    [Clickbutton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

    image1=[[UIImageView alloc] initWithFrame:CGRectMake(50,400, 60, 60)];
    image1.image=[UIImage imageNamed:@"red-circle-button.png"];

    [m_Pickercontroller.cameraOverlayView addSubview:Clickbutton];
    [m_Pickercontroller.cameraOverlayView addSubview:image1];
    [m_Pickercontroller.cameraOverlayView bringSubviewToFront:image1];

    //Hiding the Camera Control
    m_Pickercontroller.showsCameraControls=NO;

    //Presenting the imagepicker Controller to Present Modal view Controller
    [self.navigationController presentModalViewController:m_Pickercontroller animated:YES];
    }

  // Calling the Function

  - (void)viewDidLoad
  {

   [self Loadcaptureview];

   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文