0

I am using an imagePickerController to take a picture from both the gallery and using the device camera.

My problem is that if the image is selected from the gallery then I can get its "filename", if instead I use the camera to acquire the image then its "filename" is equal to "(null)".

I need the "filename" because I am uploading the picture to a Server and the .php file which I am using requires a filename for the uploaded image.

This is the code I am using

- (IBAction)addPicture:(id)sender
{
    [[[UIActionSheet alloc] initWithTitle:@"Select from:"
                                 delegate:self
                        cancelButtonTitle:@"Cancel"
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"Gallery", @"Camera", nil] showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage,  nil];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:picker animated:YES completion:NULL];
    }
    else if (buttonIndex == 1)
    {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Device has no camera."
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles: nil] show];
        }
        else
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage,  nil];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:picker animated:YES completion:NULL];
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = [info[UIImagePickerControllerEditedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];

    [_selectedImageView setImage:chosenImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        NSString * fileName = [representation filename];
        selectedImageName = [NSString stringWithFormat:@"%@", fileName];
        NSLog(@"%@", selectedImageName);
    };

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageURL
                   resultBlock:resultblock
                  failureBlock:nil];
}

This is the output I get, Image from gallery:

IMG_3383.PNG

Image from camera:

(null)

Anyone who knows how to get the "filename" for an image taken from device camera?

rambodrahmani
  • 99
  • 4
  • 12
  • when you take photo from camera, there is no file name... all you have is NSData. Either use NSData or store that image locally & then use it... – Fahim Parkar Dec 05 '14 at 20:42
  • Yeah my problem is that I can't find a way to store the image in the device Gallery and then select it from the gallery automatically. Any idea hot to do such a thing? – rambodrahmani Dec 05 '14 at 20:44
  • yes I have a way... create UIImage variable using NSData that you have and then assign that UIImage to the imageView that you have... that's it... that means you are showing image from NSData and not local (from device)... – Fahim Parkar Dec 05 '14 at 20:53
  • or save image locally with specific name (but in documents folder) and give that image path to your imageview... – Fahim Parkar Dec 05 '14 at 20:53

0 Answers0