2011-11-15 85 views

回答

1

你可以試試這種方式。

BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 


    if(hasCamera){ 

     UIActionSheet *actionSheet; 

     actionSheet = [[[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
            destructiveButtonTitle:nil 
              otherButtonTitles:@"Select from Library", @"Take a New Photo", nil] autorelease]; 

     actionSheet.actionSheetStyle = UIBarStyleBlackOpaque; 

     [actionSheet showInView:[self view]]; 
    } 

    else { 

     UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init]; 
     imagePickerController.allowsEditing = YES; 
     imagePickerController.delegate   = self; 

     imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

     [self presentModalViewController:imagePickerController animated:YES]; 

     [imagePickerController release]; 

    } 

Actionsheet委託方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    //BOOL        hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.allowsEditing = YES; 
    imagePickerController.delegate   = self; 

    if(buttonIndex == 0) 
    { 
     imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    } 
    else if(buttonIndex == 1) 
    { 
     imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    } 
    [self presentModalViewController:imagePickerController animated:YES]; 

    [imagePickerController release]; 
} 

圖像拾取委託

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    if(image) 
    { 
     [self.addPhotoButton setBackgroundImage:image forState:UIControlStateNormal]; 
    } 
    [picker dismissModalViewControllerAnimated:YES]; 

} 


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [picker dismissModalViewControllerAnimated:YES]; 

} 
+0

- 我想表明在相機屏幕只有照片庫中選擇用戶。與默認iPhone相機應用程序相同。對我而言,最後一個選擇是使用重疊屬性來定製每一件事情。如果我可以在不定製的情況下實現此功能,那將會更好。 –

相關問題