1

Hi I am trying to buil and interface which gives user options to select an existing image or take new from camera. Selecting existing image works fine and I am able to get the image file name. But when I try to take new image I am able save image to library but can not get its file name. It returns nil. Here is my code:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Dismissing camera ui...");
    [self.cameraUI dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"Getting media url...");
    NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

if(mediaURL) {
    NSLog(@"This is a video = %@", mediaURL);
} else {
    NSLog(@"This is a photo...");

    NSLog(@"Getting media type...");
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(@"Selected mediaType      : %@", mediaType);

    UIImage *originalImage;

    if (self.source == UIImagePickerControllerSourceTypeCamera) {
        // Handle a still image capture
        if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
            originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

            NSLog(@"Saving new image...");

            if (self.source != UIImagePickerControllerSourceTypePhotoLibrary) {
                UIImageWriteToSavedPhotosAlbum(originalImage, nil, nil , nil);
            }
        } else {
            // Handle a movie capture
        }
    }

    NSLog(@"Getting reference url...");
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];

    [assetLibrary assetForURL:referenceURL
                  resultBlock:^(ALAsset *asset) {
                      ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                      NSString *filename = [assetRep filename];
                      NSLog(@"File name = %@", filename);
                      if(self.selectedMediaNames == nil)
                          self.selectedMediaNames = [[NSMutableArray alloc] init];

                      [self.selectedMediaNames addObject:filename];
                      [self.tableView reloadData];
                      [self.activitIndicator stopAnimating];
                      [self.activitIndicator setHidden:true];
                  }

                 failureBlock:^(NSError *error) {
                     NSLog(@"%@", error);
                 }];
}

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

My goal is use this image name for upload the image to server.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

1 Answers1

0

I am still getting nil but I have solved my problem by saving image to a temp directory and use this path for upload:

NSString *path = [NSTemporaryDirectory()
                      stringByAppendingPathComponent:@"upload-image.tmp"];
    NSData *imageData = UIImageJPEGRepresentation(originalImage, 1.0);
    [imageData writeToFile:path atomically:YES];
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112