3

I am creating app where I am taking photo or adding through lib and saving same image on server.

Below is what I have

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    photoTaken = @"YES";
    btnImage = [info objectForKey:UIImagePickerControllerEditedImage];
    [btnImage setAccessibilityIdentifier:@"newImage"] ;
    [self.takePhoto setImage:btnImage forState:UIControlStateNormal];
    UIImageWriteToSavedPhotosAlbum(btnImage, nil, nil, nil);
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

I am using code UIImageWriteToSavedPhotosAlbum(btnImage, nil, nil, nil); to save image on iPhone because as per this question answer, if I want to save image on server I would need the file name.

Hence I would like to know how can I give file name while using UIImageWriteToSavedPhotosAlbum.

Any idea/ suggestion is appreciated.

Why I want image name is because I need to provide that image name as per above link answer.


Edit 1

Else let me know how can I save image with some name (using other formulae) so that I can use that image for my app.


Edit 2

I tried with below. Still it is not working.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    photoTaken = @"YES";
    btnImage = [info objectForKey:UIImagePickerControllerEditedImage];
    [btnImage setAccessibilityIdentifier:@"newImage"] ;
    [self.takePhoto setImage:btnImage forState:UIControlStateNormal];

    NSData *data = UIImagePNGRepresentation(btnImage);
    [data writeToFile:@"http://www.mysite.com/sama/myImages/newImage.png" atomically:YES];
    // I tried below also (thinking it will save on local)
    // [data writeToFile:@"newImage.png" atomically:YES];

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

1 Answers1

2

Build NSData in memory from the btnImage...

NSData *data = UIImagePNGRepresentation(btnImage);

Then make that your post data.

You can also write that data and upload later...

[data writeToFile:@"any_file_you_want_int_temp_dir" atomically:YES];
danh
  • 62,181
  • 10
  • 95
  • 136
  • right - that won't work. you need to make an NSURLConnection to make a web request. writing a file that way only writes locally. see http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/ to post (though watch out at the end. that blogger suggested a synchronous post, and there's a better asynch method). – danh Mar 06 '13 at 15:36
  • see here for writing locally: http://stackoverflow.com/questions/1567134/how-can-i-get-a-writable-path-on-the-iphone. The NS caches directory is probably the better choice unless the image is user generated (and needs iCloud backup). – danh Mar 06 '13 at 15:39