First you need to save the image and keep track of the filename or filepath. Here I used a time interval to create unique image names. photoImage is the UIImage that you want to store.
NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate];
NSString *photoName=[NSString stringWithFormat:@"%lf-Photo.jpeg",timeInterval];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:photoName];
NSData * photoImageData = UIImageJPEGRepresentation(photoImage, 1.0);
[photoImageData writeToFile:appFile atomically:YES];
Then you go and grab the file path using the filename that you stored or if you already stored the file path then skip to the next bit.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:photoName];
Then we make the HTTP request to send the data at said file path.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = @"POST";
[request setFile:filePath forKey:@"file1"];
[request startSynchronous];
转帖: http://stackoverflow.com/questions/7373954/send-an-image-from-the-iphone-using-asihttp-and-uiimagepicker
//
This is not possible. The UIImagePickerController will give you a UIImage*
, and you have to convert it into NSData
yourself, and store it in your local sandbox. There is no SDK-approved way to access the images in the user's photo gallery (for security reasons).
Assuming you're using SDK 3.0, here is a function on the return of the picker that will save it to the disk, in your application's documents directory (this should work, though I may have a small mistake somewhere):
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// Get the image from the result
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(image);
// Give a name to the file
NSString* imageName = @"MyImage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
}
转帖:http://stackoverflow.com/questions/1269119/uiimagepickerview-controller-image-path-iphone