2011-06-13 48 views
6

我目前正在制作iphone应用程序,用户拍摄照片或从相册中选择照片,然后在图像上放置覆盖图。用户可以缩放,旋转和保存图像。目前,我可以拍照,或选择一张专辑。至于叠加层,我只是使用UIImageView并将其放置在“接口”构建器中的层次结构顶部。对于相机,我正在使用此代码:拍照后编辑图像

-(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. 一旦选定照片,覆盖图像将变为一个在那里我”我在脸上放了一个“圆圈”,照片就像面具一样在下面。这个视图也可以全屏编辑。你可以旋转,缩放和移动图像,直到你点击完成。

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

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

非常感谢。 -Hakimo

回答

18

有办法不改变你的代码了:如果你想使用的图片是编辑后,请更改

- (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“改为”UIImagePickerControllerEditedImage“,就是这样!

+0

其中OP使用UIImagePickerControllerOriginalImage或UIImagePickerControllerEditedImage?我不明白你。 – inherithandle 2015-10-06 12:19:51

+0

@inherithandle OP是指UIImagePickerControllerDelegate的'imagePickerController:didFinishPickingMediaWithInfo:'方法。 'info'参数包含一个NSDictionary,其中包含编辑信息键,如“UIImagePickerControllerOriginalImage”和“UIImagePickerControllerEditedImage” – 2016-01-20 11:15:42

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

     } 


} 
+0

感谢您的回复Xcoder。我从未使用过动作表,所以我会试一试。使用操作手册和我写的手册有什么不同?我可以从相册中获取照片并拍照,但我不确定如何自定义编辑页面。 – hakimo 2011-06-15 01:38:12

+0

现在我看到了什么操作表。这几乎就像一个UIAlertView。我可能会使用它将图像保存到相册提示。再次感谢。 – hakimo 2011-06-15 11:45:56