iam developing one application.In that i need to get the music files from the phone library.For that iam using the MPMediaPickerCOntroller.But it doesn't fire the didpickingitem delegate method.My code like below.
- (void)viewDidLoad
{
[super viewDidLoad];
MPMediaPickerController *picker =[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt =#"Add songs to play";
[self presentModalViewController:picker animated: YES];
}
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
NSLog(#"sdfadsf");
NSLog(#"%#",mediaItemCollection);
NSArray *slist=[mediaItemCollection copy];
NSLog(#"%#",slist);
}
Do you declare the MPMediaPickerControllerDelegate in your .h?
ex:
#interface FirstViewController : UIViewController
If you do not that would explain why the method isn't firing. Also, I do not believe it will work on the simulator; only on devices.
EDIT:
-(IBAction)presentLibrary:(id)sender
{
//this is called from a button press, but you could do it in viewDidLoad
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (#"Select any song from the list", #"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
NSLog(#"didpick");
if (mediaItemCollection) {
[_mediaPlayer setQueueWithItemCollection: mediaItemCollection];
_mediaCollection = mediaItemCollection;
}
[mediaPicker dismissModalViewControllerAnimated:YES];
}
Read from here The Music Player Framework in iPhone
Related
Edit - I solved this myself - see the notes at the bottom
When using iOS7 on Xcode 5, I am using an option to take an image from a camera, or from the photo library, once the image is chosen (or a new picture taken) the view should flip over to the next screen.
This does not happen on the iPhone running iOS7, it works fine on the iPad, but the method is slightly different, but it does appear to be iPhone only problem on iOS7.
here is the code used, for example, on the choose image from library function;
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
[self presentModalViewController: cameraUI animated: YES];
}
}
Also, the code once picker is finished;
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
//Disable buttons
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self disableButtons];
//Get image
self.originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
//Dismiss
if(_popover)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[_popover dismissPopoverAnimated:YES];
_popover = nil;
}
}
else
[picker dismissModalViewControllerAnimated: YES];
//Next
[self performSelector: #selector(nextScreen) withObject:nil afterDelay:0.5];
}
I fixed this by switching out;
[picker dismissModalViewControllerAnimated: YES];
With
[picker dismissViewControllerAnimated:NO completion:nil];
First you need to make sure that logic reaches the correct place it's intended to, try setting a breakpoint or NSLog before the iPhone's specific line, try this (You also missed curled braces, added them here) :
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
NSLog(#"Checkpoint !");
[self presentModalViewController: cameraUI animated: YES];
}
} }
Im newish to Objective-C and I want to create an app with that will eventually take pictures with the camera but with images overlaid. But I thought I would start with a simple camera app just to get the base for it. I have followed a tutorial for creating a app that you can take pictures with and access image library. BUT when I build it to my iPhone 4s it just loads a white screen. If any one could take a look at the code, but I think its right and offer an answer it would be great thanks.
//ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto:(UIButton *)sender;
- (IBAction)selectPhoto:(UIButton *)sender;
#end
//ViewController.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#end
I have used AVCaptureSession for capture photo in iPhone application. As well as I want to take an image from PhotosLibrary too.
I've implemented UIImagePickerController control and its all methods.
I can open library, But I can't get photo which I've selected. As well as not calling PickerView's methods. No response from those methods.
Is there any different way to implement UIImagePickerController control with AVCaptureSession for photos library?
Please tell me solution.
Or suggest me alternative solution instead of Overlay as I want both functionality with dynamic controls.
Thanks in advance.
For open photo library use this:
-(IBAction)pickphoto:(id)sender
{
[self startMediaBrowserFromViewController: self usingDelegate: self];
}
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate{
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie,kUTTypeImage, nil];
mediaUI.allowsEditing = YES;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES;
}
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
thumbnail = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[self dismissModalViewControllerAnimated:YES];
}
And for capture a Image use this:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
I have implemented picture taking while pressing UI Button butwhen ever i pressed the button got app crashed.
Here is the source code.
.h file
#interface Camera : UIViewController
<UIImagePickerControllerDelegate>
{
UIImagePickerController *mPicture;
}
#property (nonatomic, retain) UIImagePickerController *mPicture;
.m file
#implementaion Camera
#synthesize mPicture;
-(void)pictureButtonPushed
{
UIImagePickerControllerSourceType mType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:mType])
{
mPicture.sourceType = mType;
[self presentModalViewController:mPicture animated:YES];
}
}
Thanks in advance
Try this one
Hope it will help :)
-(IBAction)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
-(IBAction)chooseFromLibrary
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[myImageView setImage:image]; // "myImageView" name of any UImageView.
}
Here is the code for what you want
- (void)cameraPressed
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle:#"Set a Prifile Picture"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Choose From Library",#"Take a New Photo",nil];
[menu showInView:[self.navigationController view] ];
}
// actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
#try {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
else {
[appDelegate showAlertWithTitle:#"Info" message:#"This function needs a camera which is only available on the iPhone or iPod."];
}
}
#catch (NSException *e) {
}
}
if (buttonIndex == 0) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.navigationBar.barStyle = UIBarStyleBlack;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = NO;
[appDelegate.objList setHidden:TRUE];
appDelegate.strRefreshCamera = #"notupdate";
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//flag = TRUE;
[self dismissModalViewControllerAnimated:YES];
//[appDelegate showLoadingView];
UIImage *capturedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
Use this image object where ever you want to use.
What function should I implement to return a selected picture from gallery?
Use UIImagePickerController. In this example, I'm using the built-in image cropper as well.
- (void) showImage {
UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.mediaTypes = [NSArray arrayWithObject: (NSString *) kUTTypeImage];
[self presentModalViewController: imagePickerController animated: YES];
}
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage];
self.editedImage = image;
[self dismissModalViewControllerAnimated: YES];
}
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[self dismissModalViewControllerAnimated: YES];
}
This will allow the user to pick an image. If you don't want the cropping, set imagePickerController.allowsEditing = NO and use the key UIImagePickerControllerOriginalImage to get the resulting image back.