拍照后编辑图像

时间:2011-06-13 10:04:21

标签: iphone ios uiimagepickercontroller

我正在制作一款iphone应用,用户可以在其中拍摄照片或从相册中选择照片,然后在图像上方放置一个叠加层。然后,用户可以缩放,旋转和保存图像。目前,我可以拍照,或者为相册选择一张。至于叠加层,我只使用了UIImageView并将它放在Interface builder中的层次结构之上。对于相机,我正在使用此代码:

-(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];
}

我现在遇到的问题是在拍摄后编辑照片。如何自定义相机以表现如下?:

  1. 选择使用相机或从相册中获取照片

  2. 一旦选择,叠加图像将变为我在脸部放置“圆圈”的图像,照片将像面具一样位于下方。此视图也可以全屏编辑。您可以旋转,缩放和移动图像,直到单击完成为止。

  3. 我在手册中已经阅读了这部分内容,但我似乎无法理解如何使用它。 http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

    希望有人能指出我正确的方向。

    非常感谢。 -Hakimo

2 个答案:

答案 0 :(得分:19)

有一种方法可以改变你的代码:

- (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];
}

如果您想使用编辑后的图像,请更改" UIImagePickerControllerOriginalImage" to" UIImagePickerControllerEditedImage",那就是它!

答案 1 :(得分:0)

对于Q1,是否可以实施操作表,以便用户可以选择使用相册中的照片或拍摄新照片。使用动作表功能如下所示:

-(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];

        }


}