iPhone-UIImagePickerController - > Zapisz obraz w folderze aplikacji
Mam aplikację na iPhone ' a używającą UIImagePickerController
. Jako sourceType
Mam
- UIImagePickerControllerSourceTypephotolibrary
- UIImagePickerControllerSourceTypecamera
- UIImagePickerControllerSourceTypesavedphotosalbum
Więc użytkownik może zrobić zdjęcie lub wybrać jedno z biblioteki zdjęć ze zdjęć lub zdjęć z aparatu.
Obraz zostanie wyświetlony w widoku UIImageView. Obraz powinien zostać zapisany, jeśli użytkownik zamknie aplikację. Więc dla pól tekstowych używam NSUserDefaults
. Wiem, że nie jest to dobry sposób, aby zapisać obraz wewnątrz NSUSerDefaults z NSData, więc chcę zapisać / skopiować obraz do folderu, który jest kontrolowany przez moją aplikację podobny do NSUserDefaults.
NSUserDefaults
i odczytać go przy starcie aplikacji.
Z góry dziękuję i pozdrawiam.
4 answers
Możesz użyć poniższego kodu w UIImagePickerControllerDelegate
delegate implementation
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
To wszystko
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-10 14:12:15
W zależności od formatu pliku, który chcesz zapisać, możesz użyć
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
Lub
[UIImageJPEGRepresentation(image) writeToFile:path atomically:YES];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-26 03:53:33
Jest to kod do zapisania interfejsu użytkownika w katalogu dokumentu. Możesz użyć tego kodu w metodzie didFinishPickingImage delegate:
// Create paths to output images
NSString *pngPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory();
stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
EDIT
Możesz również użyć:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Aby znaleźć ścieżkę do katalogu dokumentu aplikacji, zamiast NSHomeDierctory.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-07 05:31:29
Aktualizacja odpowiedzi knuku dla Swift 3.0
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//obtaining saving path
let fileManager = FileManager.default
let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let imagePath = documentsPath?.appendingPathComponent("image.jpg")
// extract image from the picker and save it
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
try! UIImageJPEGRepresentation(pickedImage, 0.0)?.write(to: imagePath!)
}
self.dismiss(animated: true, completion: nil)
}
Tutaj obraz jest zapisany jako jpeg, ale możesz go również zapisać jako png. parametr 0.0 oznacza kompresję i jest to najniższa jakość, jeśli chcesz uzyskać najlepsze wykorzystanie 1.0.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-17 17:09:08