2011-06-13 84 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