Edit image after taking a picture

2020-05-19 03:17发布

I'm currently making an iphone app where the user takes a photo or select it from an album, then an overlay is placed over the image. The user can then scale, rotate and save the image. Currently, I can take pictures, or choose one for the album. As for the overlay, I just used UIImageView and placed it on top of the hierarchy in Interface builder. For the camera, I'm using this code:

-(IBAction)getPhoto:(id)sender  {

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


if((UIButton *) sender == choosePhotoBtn)   {
    // Set source to photo albums
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}

else    {
    // Set source to camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = YES;
}

// Delegate is self
imagePicker.delegate = self;

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

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

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info   {
// Dismiss modalviewcontroller
[picker dismissModalViewControllerAnimated:YES];

// Displaying image to the imageView
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Access the uncropped image from info dictionary
UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


// Save Image
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

[picker release];
}

The problem I'm having right now, is editing the photo after it's being taken. How do I customise the camera to behave like this?:

  1. Choose either use the camera or getting the photo from the album

  2. Once chosen, the overlay image will change to a one where I've put a "circle" in the face, and the photo will be underneath like a mask. This view will also be able to edit in full screen. You can rotate, scale and move the image until you click done.

I've read this part in the manual but I can't seem to understand how to use it. http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

Hope someone can point me to the right direction.

Thanks very much. -Hakimo

2条回答
劳资没心,怎么记你
2楼-- · 2020-05-19 03:46

there's a way don't change your code much:

- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

[imagePicker setAllowsEditing:YES];
[imagePicker setDelegate:self];

//place image picker on the screen
[self presentViewController:imagePicker animated:YES completion:nil];
}

If you want to use the image that after editing, please change "UIImagePickerControllerOriginalImage" to "UIImagePickerControllerEditedImage", that's it!

查看更多
趁早两清
3楼-- · 2020-05-19 04:06

For Q1, is it possible to implement an action sheet, so that the user can choose to use the photo from an album or take a new pic.? with the action sheet function as something like the following:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }

    else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.allowsEditing = YES;
            picker.delegate = self;
            [self presentModalViewController:picker animated:YES];
            [picker release];
        }
        else {
            UIAlertView *noCameraMsg = [[UIAlertView alloc] initWithTitle:@"no camera on this phone" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [noCameraMsg show];
            [noCameraMsg release];

        }


}
查看更多
登录 后发表回答