相机图像选择器程序中的iOS错误



我会创建一个带相机的应用程序,但是当我在 ImageViewController.m 中编写此代码时xcode:预期的METODY BODY和"."我该如何解决这个问题?谢谢

-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
            BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
            //if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
            if (!truefalse || (delegate == nil) || (controller == nil)) {
                NSLog(@"no can do, delegate/camera/view controller doesn't exist!");
                return NO;
            }
            UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
            cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
            cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
            cameraController.allowsEditing = NO;
            cameraController.delegate = delegate;

它说你根本没有方法。尝试更改为以下内容(请注意方法名称后和所有代码之后的大括号):

-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{ 
            BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
            //if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
            if (!truefalse || (delegate == nil) || (controller == nil)) {
                NSLog(@"no can do, delegate/camera/view controller doesn't exist!");
                return NO;
            }
        UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];

            cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
            cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
            cameraController.allowsEditing = NO;
            cameraController.delegate = delegate;
}

你能试试这个吗?

- (IBAction)presentPicker:(id)sender {
    // **********************************************
    // * Show action sheet that will allow image selection from camera or gallery
    // **********************************************
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Image from Camera", @"Image from Gallery", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.alpha=0.90;
    actionSheet.tag = 1;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
 }

  #pragma mark - Image picker methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:  (NSInteger)buttonIndex {
    switch (actionSheet.tag) {
              case 1:
                switch (buttonIndex) {
              case 0:
                  [self showCameraImagePicker];
                  break;
               case 1:
                    [self showGalleryImagePicker];
                  break;
             }
            break;
           default:
             break;
     }  
 }

    - (void)showCameraImagePicker {

              #if TARGET_IPHONE_SIMULATOR
               UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Simulator"   message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
    #elif TARGET_OS_IPHONE
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
       picker.delegate = self;
       picker.allowsEditing = YES;
       picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:NULL];
       // [(UIViewController *)self.delegate presentModalViewController:picker  animated:YES];
      #endif

     }
   - (void)showGalleryImagePicker {
         UIImagePickerController *picker = [[UIImagePickerController alloc] init];
         picker.delegate = self;
         picker.allowsEditing = YES;
         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:NULL];
   //   [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
       }
   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
  //  [picker dismissModalViewControllerAnimated:NO];
      [picker dismissViewControllerAnimated:YES completion:NULL];
      photo.image = image;
     //[self presentImageCropperWithImage:image];

    }
   -(void)imagePickerController:(UIImagePickerController*)picker   didFinishPickingMediaWithInfo:(NSDictionary*)info {
 //  [picker dismissModalViewControllerAnimated:NO];
     [picker dismissViewControllerAnimated:NO completion:nil];
  // Extract image from the picker
     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]){
       UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        photo.image = image;
        [picker dismissViewControllerAnimated:YES completion:NULL];
       // [self presentImageCropperWithImage:image];
    }
   }

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
   [picker dismissViewControllerAnimated:YES completion:NULL];
}

最新更新