简体   繁体   English

如何将照片库或相机中的图像添加到我的Ipad应用程序?

[英]how to add image from photo library or camera to my Ipad application?

I want to add images from photo library or by using camera to my ipad application. 我想从照片库中添加图像或使用相机添加到我的ipad应用程序中。 I had used same coding as iphone . 我使用了与iphone相同的编码。 But the application is crashing. 但是应用程序崩溃了。 I think this is not the right way to add picture from library to ipad application. 我认为这不是从库添加图片到ipad应用程序的正确方法。 If anyone knows please help me. 如果有人知道请帮助我。

-(void)ctnPhotoSelection
{
    isMyCtView = YES;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                             delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Take Photo With Camera", @"Select Photo From Library", @"Cancel", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
    actionSheet.destructiveButtonIndex = 1; 
    [actionSheet showInView:self.view]; 
    [actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self TakePhotoWithCamera];
    }   
    else if (buttonIndex == 1)
    {
        [self SelectPhotoFromLibrary];
    }

    else if (buttonIndex == 2)
    {
        NSLog(@"cancel");
    }
}

-(void) TakePhotoWithCamera
{
    [self startCameraPickerFromViewController:self usingDelegate:self];
}

-(void) SelectPhotoFromLibrary
{
    [self startLibraryPickerFromViewController:self usingDelegate:self];
}

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

- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject  
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.allowsEditing = YES;
        picker.delegate = self;
        [controller presentModalViewController:picker animated:YES];
        [picker release];
    }
    return YES;  
}  

- (BOOL)startLibraryPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject  
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 
    {
        UIImagePickerController *picker1 = [[UIImagePickerController alloc]init];
        picker1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker1.allowsEditing = YES;
        picker1.delegate = self;
        [controller presentModalViewController:picker1 animated:YES];
        [picker1 release];
    }
    return YES;  
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    UIImage * img = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    [self useImage:img];
    [[picker parentViewController] dismissModalViewControllerAnimated:NO];
    UINavigationController* navController = self.navigationController;
    UIViewController* controller = [navController.viewControllers objectAtIndex:0];
    [controller dismissModalViewControllerAnimated:NO];
}

-(void)useImage:(UIImage *)theImage
{
    NSData *addImageData = UIImagePNGRepresentation(theImage);
    IpadAppDelegate *appDelegate=(IpadAppDelegate*)[ [UIApplication sharedApplication] delegate];

    if(isMyCtView == YES)
    {
        isMyCtView = NO;
        appDelegate.imageCtData = addImageData;
        appDelegate.isctFromDevice = YES;
        appDelegate.isctnCntrl = YES;
        [self.view sendSubviewToBack:myCtnView];
    }

    [self reloadView];
}

Crash Log 崩溃日志

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
*** Call stack at first throw:
(
 0   CoreFoundation                      0x3587a987 __exceptionPreprocess + 114
 1   libobjc.A.dylib                     0x34a8249d objc_exception_throw + 24
 2   UIKit                               0x34378317 UIImagePickerEnsureViewIsInsidePopover + 258
 3   PhotoLibrary                        0x3499893b -[PLLibraryView didMoveToWindow] + 86
 4   UIKit                               0x341bf76b -[UIView(Internal) _didMoveFromWindow:toWindow:] + 698
 5   UIKit                               0x341bf583 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 210
 6   UIKit                               0x341bf433 -[UIView(Hierarchy) _postMovedFromSuperview:] + 106
 7   UIKit                               0x341aa82f -[UIView(Internal) _addSubview:positioned:relativeTo:] + 678
 8   UIKit                               0x341aa57f -[UIView(Hierarchy) addSubview:] + 22
 9   UIKit                               0x341fc8a9 -[UINavigationTransitionView transition:fromView:toView:] + 416
 10  UIKit                               0x341fc6fd -[UINavigationTransitionView transition:toView:] + 20
 11  UIKit                               0x341ef8cf -[UINavigationController _startTransition:fromViewController:toViewController:] + 1274
 12  UIKit                               0x341ef35f -[UINavigationController _startDeferredTransitionIfNeeded] + 182
 13  UIKit                               0x341ef2a3 -[UINavigationController viewWillLayoutSubviews] + 14
 14  UIKit                               0x341ef23f -[UILayoutContainerView layoutSubviews] + 138
 15  UIKit                               0x341b80cf -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 26
 16  CoreFoundation                      0x35818bbf -[NSObject(NSObject) performSelector:withObject:] + 22
 17  QuartzCore                          0x31075685 -[CALayer layoutSublayers] + 120
 18  QuartzCore                          0x3107543d CALayerLayoutIfNeeded + 184
 19  QuartzCore                          0x3106f56d _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 212
 20  QuartzCore                          0x3106f383 _ZN2CA11Transaction6commitEv + 190
 21  QuartzCore                          0x31092f9d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 52
 22  CoreFoundation                      0x3580ac59 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16
 23  CoreFoundation                      0x3580aacd __CFRunLoopDoObservers + 412
 24  CoreFoundation                      0x358020cb __CFRunLoopRun + 854
 25  CoreFoundation                      0x35801c87 CFRunLoopRunSpecific + 230
 26  CoreFoundation                      0x35801b8f CFRunLoopRunInMode + 58
 27  GraphicsServices                    0x320c84ab GSEventRunModal + 114
 28  GraphicsServices                    0x320c8557 GSEventRun + 62
 29  UIKit                               0x341dc329 -[UIApplication _run] + 412
 30  UIKit                               0x341d9e93 UIApplicationMain + 670
 31  IpadComicBuilder                    0x00002537 main + 70
 32  IpadComicBuilder                    0x000024b8 start + 52
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
(gdb)

The answer is in your error message. 答案在您的错误消息中。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

On an iPad, you can't just use the iPhone method of selecting an image. 在iPad上,您不能只使用iPhone方法选择图像。 You need to use a UIPopoverController , you can pass it your UIImagePickerController with: 你需要使用UIPopoverController ,你可以通过它传递你的UIImagePickerController:

UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController: imagePicker];

Then implement the UIPopoverControllerDelegate protocol in your class. 然后在您的类中实现UIPopoverControllerDelegate协议。

You can find an example of this here . 你可以在这里找到一个例子。

- (void)buttonTap {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil
                                                         delegate: self
                                                cancelButtonTitle: @"Cancel"
                                           destructiveButtonTitle: nil
                                                otherButtonTitles: @"Take a new photo",
                                                         @"Choose from existing", nil];
                                                   [actionSheet showInView:self.view];
}

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

    int i = buttonIndex;

        switch(i) {

    case 0:
    {

      //Code for camera  

    }
        break;
    case 1:
    {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
       self.popover = [[UIPopoverController alloc] initWithContentViewController: picker];
        self.popover.delegate =self;
        [self.popover presentPopoverFromRect:self.view.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

    default:
        // Do Nothing.........
        break;

    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"Picker returned successfully.");

    UIImage *selectedImage;

    NSURL *mediaUrl;

    mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];

    if (mediaUrl == nil) {

    selectedImage = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
    if (selectedImage == nil) {

        selectedImage= (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
        NSLog(@"Original image picked.");

         }
    else {

        NSLog(@"Edited image picked.");

         }    

    }

    [picker dismissModalViewControllerAnimated:YES];


    [self.imageButton setImage:selectedImage forState:UIControlStateNormal];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [self dismissModalViewControllerAnimated:YES];
}

for Swift 4 对于Swift 4

get image from camera 从相机获取图像

let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .camera
self.present(picker, animated: true, completion: nil)

get image from library 从图书馆获取图像

let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)

delegate function 委托功能

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  var result = false

  if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
      //do something to image here ...

      picker.dismiss(animated: true, completion: nil)
  }
}

In your view controller, don't forget to inherit class UIImagePickerControllerDelegate , UINavigationControllerDelegate 在你的视图控制器中,不要忘记继承类UIImagePickerControllerDelegateUINavigationControllerDelegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测照片库中的图像是由iPhone相机拍摄还是导入 - how to detect if image from photo library was taken by iPhone camera or imported 附加相机或照片库中的图像缩略图 - Attaching image thumbnails from camera or photo library 对于iPad开发人员:如何在iPad的UIImageView中查看从照片库的弹出菜单中选择的图像 - For iPad Developers: How to view an image selected from photo library’s popover in a UIImageView, iPad iPhone:如何通过iPhone照片应用程序等“编辑”功能从照片库或相机中获取图像 - iPhone:How to fetch image from photo library or camera with “edit” funcitionality like iPhone photo app 是否可以通过使用图像名称从iPad照片库获取图像 - Is it possible to get an image from iPad Photo Library by using the image name 如何在PHAsset中添加图像并将其从照片库中删除? - How to add image in PHAsset and delete it from photo library? iPad模拟器iOS6,未从UIImagePickerController照片库获取图像 - iPad simulator iOS6, not getting image from UIImagePickerController photo library 获取从相机/照片库拍摄的图像的名称 - Get the name of an image taken from camera/photo library iPad应用程序中的库错误 - Library error in my iPad application 如何通过iOS 4.0使用照片库从我的nativa应用程序设置壁纸? - how can i set wallpaper from my nativa application thru iOS 4.0 using Photo Library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM