1

I've searched how to get the name of the saved image takes from the camera, but i didn't find something simple.

this is my code :

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

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera){
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
     {
         NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
         [library assetForURL:assetURL resultBlock:^(ALAsset *asset )
          {
              NSLog(@"we have our ALAsset!");
              NSLog(@"%@", assetURL);

          }
                 failureBlock:^(NSError *error )
          {
              NSLog(@"Error loading asset");
          }];
     }];
}

}

I you have the answer, i will happy to test it .

Thanks in advance.

2 Answers2

1
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *resourceURL;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];

image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

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

NSString *imageName = [imagePath lastPathComponent];

resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];


NSData *imageData;
NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];

if ([extensionOFImage isEqualToString:@"jpg"])
{
    imageData =  UIImagePNGRepresentation(image);
}

else
{
    imageData =    UIImageJPEGRepresentation(image, 1.0);
}

int imageSize=imageData.length/1024;
    NSLog(@"imageSize--->%d", imageSize);
if (imageName!=nil) {
   NSLog(@"imageName--->%@",imageName);
}
else
{
   NSLog(@"no image name found");
}
}

If you are using ASSerts

-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
[self dismissViewControllerAnimated:YES completion:^{

    if (assets.count < 1) return;

    //self.pageControl.numberOfPages = assets.count;

    int index = 0;

    for (ALAsset *asset in assets) {
        //  NSString *imageName = [[asset defaultRepresentation] filename];

        UIImage *image = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
        // (@"%@", [[asset defaultRepresentation] filename]);
        NSData *imageData = UIImagePNGRepresentation(image);
        int imageSize=imageData.length/1024;
    (either)    
   NSURL *imagePath = [asset objectForKey:@"UIImagePickerControllerReferenceURL"];
    //  NSURL *imagePath = [NSURL URLWithString:[asset ob]];
         NSString *imageName = [imagePath lastPathComponent];
    (or)
    NSLog(@"%@",[[asset defaultRepresentation] filename]);
        index++;
    }

}];
}
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
  • Thanks for your help, i don't use assets. I had tried your solution (the first) but the photo is not saving :( –  Dec 23 '13 at 12:55
  • After getting imageData print it once in console and see how much data was printed in bytes . – Charan Giri Dec 23 '13 at 12:59
  • after this statement image =[info objectForKey:@"UIImagePickerControllerOriginalImage"]; write if(image){ NSLog(@"Have image");} else{NSLog(@"no Image")} if the statement is true share your code of showing image if the statement is false let me know i'll cross check again with my code and will get back to you – Charan Giri Dec 23 '13 at 13:03
  • I had added the nslog and it returns that i've an image. –  Dec 23 '13 at 13:11
  • can you please share you code how you are displaying image. – Charan Giri Dec 24 '13 at 04:31
  • I don't display the image, i just take the picture and save it to see in the default photo app. –  Dec 24 '13 at 08:44
  • try to display the image in your app so that you can be sure image is there.. if the image is displayed in you app. show me you code how you are saving it to photo app – Charan Giri Dec 24 '13 at 08:47
  • My code to save the photo is in my question. ;) and it works, i can see my photo in the photo app. –  Dec 24 '13 at 09:00
  • So your app requirement is done.. – Charan Giri Dec 24 '13 at 09:10
  • no because i need the name of the picture to save it in a core data object :( –  Dec 24 '13 at 09:13
  • aren't you getting image name in NSLog(@"imageName--->%@",imageName); – Charan Giri Dec 24 '13 at 09:17
  • yes. you need to add few files for that you can get it in internet. Make sure you add frameworks and deployment target not less than 5.0 – Charan Giri Dec 24 '13 at 09:47
  • i've found another way! the saving is ok and i think I'm on the good way, can you see my first edited post ? –  Dec 24 '13 at 09:54
  • yes i have seen it ALAsset will fix the issue.. Use that code. – Charan Giri Dec 24 '13 at 10:14
  • With this code, the saving works but what can I use to get name of the saved photo? –  Dec 24 '13 at 10:16
  • NSString *imageName = [imagePath lastPathComponent]; – Charan Giri Dec 24 '13 at 10:22
  • but i haven't variable imagepath. –  Dec 24 '13 at 10:23
  • Compare this [method]-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets which i have written with the other one. By using [imagePath lastPathComponent]; you will get name of image – Charan Giri Dec 24 '13 at 10:25
  • Yeah, thanks! however all pictures are named asset.JPG :( –  Dec 24 '13 at 10:30
  • try this once [[asset defaultRepresentation] filename]; – Charan Giri Dec 24 '13 at 11:27
  • The problem is that i haven't asset, i've just an ALAssetsLibrary object, so i can't use defaultRepresentation. –  Dec 24 '13 at 13:09
  • If it is not very urgent i'll get back to you with solution on 26th – Charan Giri Dec 24 '13 at 13:18
0

Try following code. I have used this in one of my previous project and its working for me :

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

  _selectedImage = info[UIImagePickerControllerEditedImage];
  self.photoView.image = _selectedImage;

  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];

  [picker dismissViewControllerAnimated:YES completion:NULL];

}
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
rambo
  • 61
  • 8
  • thanks for your help but the filename is always null and the photo is not saving :( –  Dec 23 '13 at 12:56