iPad UIImagePicker Problem ! - ipad

iam trying import an image from photos library but when i press the import button program crashes and received SGIBART !! but my code works fine on iPhone why ?
here is my code :
.h :
#interface CameraViewController : UIViewController <UIImagePickerControllerDelegate ,UINavigationControllerDelegate> {
UIImagePickerController *ipc;
UIImageView * image1;
#property (.......................;
}
.m :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image1.image = [[info objectForKey:UIImagePickerControllerOriginalImage]retain];
[[picker parentViewController]dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[[picker parentViewController]dismissModalViewControllerAnimated:YES];
[picker release];
}
-(IBAction) importImage1 {
ipc = [[UIImagePickerController alloc]init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:ipc animated:YES];
}

You're crashing because you're releasing picker, but you didn't retain it. You should remove the calls to [picker release].
You should switch to using accessors for all of your ivars. Accessors and dealloc are generally the only places you should retain or release your ivars. picker is not your ivar and you didn't create it, so you shouldn't be releasing it at all.
You should spend some time with the Memory Management Programming Guide. You can get the short version at Three Magic Words.

On the iPad iOS 3.2 a UIImagePickerController must be presented in a popover, not as a modal view.
check this out :
http://www.cocos2d-iphone.org/forum/topic/6108

Related

Xcode 8 UIImagePickerController frozen

I just run into this problem. I can call UIImagePickerController as usual but when I pick an Image(taking a photo or photo library), the "use photo" button and "retake" button don't work, and the UI just freezes.
I have done some debugging and found the code won't get into the delegate method.
I didn't change any code about UIImagePickerController. Everything works just fine before. So I'm wondering why this happened and how to fix this bug?
Thanks a lot!
Here is the code :
UIImagePickerController * imgPicker = [[UIImagePickerController alloc] init];
[imgPicker setDelegate:self]
[imgPicker setAllowsEditing:YES];
[imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self.navigationController presentViewController:imgPicker animated:YES completion:^{
}];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
I figure this out.This is caused by using the wrong delegate method.the one I was using has been DEPRECATED.
We should use this one
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
and the image information is in the info dictionary.You can get the image in this way
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
From xcode 8 with ios 10 you have to add some key value to the info.plist for accessing the photos using UIImagepicker Controller
Key : Privacy - Photo Library Usage Description
Value : This app requires access to the photo library

IOS 10 UIImagePickerController Delegate Not Being Called

I have setup the following to select an image whilst using an IPad. The problem is that the delegate never seems to get called. I've placed breakpoints in but they are never activated.
.H
#interface HomeViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
.M
- (IBAction)loadImage:(id)sender {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePickerController.allowsEditing = NO;
self.imagePickerController.delegate = self;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Can anyone see the issue?
Thanks
if you used Xcode8 to run the project, please check the project's info.plist, make sure there is a key for "Privacy Photo library usage".
your code is right, maybe the problem is the info.plist.
I had a similar issue and it turned out that the picker was being garbage collected as soon as the image was picked, so the delegate wasn't called.
I needed to ensure that a strong reference to the picker was made before presenting it.
Once that was done, it worked fine.

UIImagePickerController not taking photos and freezes after I open second time

I have simple singleton class that takes care of creating and presenting UIImagePickerController. It worked just fine until few hours ago. All I did was moving the code from ViewController to separate singleton class (relevant code at the end of post). When I tried to undo all changes to get back to previous state, the problem persisted.
Whenever I present camera, I get warning (not sure if it is related or not)
"Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates."
Then, when I hit shutter button, nothing happens, no sound, no focus, no photo taken, no callback back to my app.
When I hit cancel, I get the callback and dismiss the controller without problems.
Now when I open camera again, the screen practically freezes, I get the controls and everything, but the screen doesn't update (actually it does, but only once or twice a minute)
#interface AddPhotoManager()
#property (nonatomic, weak) UIViewController *controller;
#property (nonatomic, strong) UIImagePickerController *imagePickerController;
#end
#implementation AddPhotoManager
+ (instancetype)sharedInstance
{
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)addNewPhotoFromController:(UIViewController*)controller
{
self.controller = controller;
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo library", nil];
[sheet showInView:self.controller.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0 || buttonIndex == 1) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// Will get here on both iOS 7 & 8 even though camera permissions weren't required
// until iOS 8. So for iOS 7 permission will always be granted.
if (granted) {
// Permission has been granted. Use dispatch_async for any UI updating
// code because this block may be executed in a thread.
dispatch_async(dispatch_get_main_queue(), ^{
[self presentPickerController:buttonIndex == 0];
});
} else {
// Permission has been denied.
}
}];
}
}
- (void)presentPickerController:(bool)camera
{
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.sourceType = camera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.imagePickerController.delegate = self;
[self.controller presentViewController:self.imagePickerController animated:false completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:true completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
// TODO: upload the image
self.imagePickerController = nil;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:true completion:nil];
self.imagePickerController = nil;
}
The singleton is used simply by calling
[[AddPhotoManager sharedInstance] addNewPhotoFromController:self];
from any view controller
EDIT:
storing imagePickerController in strong property doesn't seem to be necessary, but I wasn't sure so I tried it. However, results are same in both cases.
EDIT 2:
I tried exact same code in new test app and everything worked without problems, so it must be some interference with some other parts of the app? Any ideas what it could be? I tried deleting the app to reset permissions, even changed bundle Id, but nothing helped

Post more than one photo - two UIImagePickerController's

I'm having a slight issue here.
I have a basic view which has two UIButtons and two UIImageViews, I would like my users to be able to take two different photos, and these to be attached to their respective UIImageView.
Right now this works fine for the first UIButton/image, but not with a second.
.h
#interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
// First button/imageview
UIImagePickerController *picker;
UIImage *image;
// Second button/imageview
UIImagePickerController *picker2;
UIImage *image2;
}
.m
-(IBAction)TakePhoto
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image2 = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageField2 setImage:image2];
[self dismissViewControllerAnimated:YES completion:NULL];
}
This all works great but my question is how can I hook up the second picker to a new imagePickerController method so that my user can take two different photos on this view? I know that if I created a new action with the picker2 information it will still point to the one imagePickerController method, creating a second method and renaming this to suit does not work.
Any advice & help would be greatly appreciated.
Jamie
You don't need two UIImagePickerControllers to do it.
Take a look at takePicture method of UIImagePickerController which programatically initiates still image capture.
You can initiate additional captures after receiving imagePickerController:didFinishPickingMediaWithInfo: delegate callback.
PhotoPicker is a sample project from Apple which does exactly what you are trying to do.
Hope it helps.

App using UIImagePickerController freezes when selecting "Use Photo" after taking a photo

I'm working on a simple photo and video capture app right now. The app successfully allows the user to take a photo or video on a button press. However, as soon as you finish taking your photo or video, it gives 2 options: "Retake" and "Use Photo" or "Use Video", depending on which one you're using.
If the user taps "Retake" then it just let's them retake a new photo or video, but when the user taps "Use photo" or "Use video" the screen freezes.
If I exit the frozen app and go look in my camera roll, the photo I just took using the app has successfully been saved. So when you tap the "Use Photo" button it does successfully save to the camera roll despite the fact that the app screen freezes.
I was told by someone that "it freezes because it takes some time to save the photo and it is running on main thread. You can use the ALAssetLibrary to fix the freezing issue."
However, I am not familiar with ALAssetLibrary or the theory of why it would be helpful in this situation. I just skimmed through some of it's documentation and I am still lost.
Any help would be greatly appreciated.
Here is the code that I have so far:
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
#property UIImagePickerController * pickerController;
-(IBAction)showCameraUI;
#end
ViewController.m:
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
//The above are the 2 delegates required for creating a media capture/camera app.
- (void)imagePickerController:pickerController didFinishPickingMediaWithInfo:(NSDictionary *)info;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
NSLog(#"%ld", (long)UIImagePickerControllerSourceTypeCamera);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)showCameraUI{
UIImagePickerController * pickerController = [[UIImagePickerController alloc]init];
pickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void)imagePickerController:pickerController didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// saves the photo you just took
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
}
}
- (void)imagePickerControllerDidCancel:pickerController
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end

Resources