-1

I'm using the following code to retrieve an image.

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
NSString *base64String = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  1. When I try to print the path, its printing the entire file path but How can I get the file name out of NSURL?
  2. After accessing the path, I wish to convert the image to base 64 using the path. How can I do so?
Atanu Mondal
  • 1,714
  • 1
  • 24
  • 30
  • Why are you using `valueForKey:`? What does "I wish to convert the image to base 64 using the path" mean? – trojanfoe May 23 '16 at 13:41

4 Answers4

1

you can get the file name using

NSString *theFileName = [[string lastPathComponent] stringByDeletingPathExtension];

And use below code for base64 conversation

- (NSString*)base64forData:(NSData*) theData
{
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
JP_Mob
  • 114
  • 7
0

You can get name like,

 NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

 NSString *imageName = [imagePath lastPathComponent];
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

I suspect you're passing the wrong key

NSURL *url = info[UIImagePickerControllerMediaURL];
NSString *fileName = url.lastPathComponent;
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Try this i hope it would be helpful!! import AssetsLibrary in your file:

#import <AssetsLibrary/AssetsLibrary.h>

And, in

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

put

    // get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
};

// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

And another Way you can do like that!!!

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)

let image = info[UIImagePickerControllerOriginalImage] as UIImage
let data = UIImagePNGRepresentation(image)
data.writeToFile(localPath, atomically: true)

let imageData = NSData(contentsOfFile: localPath)!
let photoURL = NSURL(fileURLWithPath: localPath)
let imageWithData = UIImage(data: imageData)!

picker.dismissViewControllerAnimated(true, completion: nil)

}

Sanjeet Verma
  • 551
  • 4
  • 15