Jak otworzyć aparat w iPhone App ... Podobnie jak przy użyciu imagepickerview możemy otworzyć library..how otworzyć aplikację aparatu, dzięki czemu możemy zrobić zdjęcie ..otwarcie w iPhone App programowo
Odpowiedz
Musisz użyj UIImagePickerController.
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
Trzeba wdrożyć metodę imagePickerController:didFinishPickingMediaWithInfo:
UIImagePickerControllerDelegate
a następnie zapisać UIImage do gdziekolwiek chcesz, bez względu na nazwę pliku ze chcesz, stosując metody NSFileManager.
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
ten kod wywoła kamerę urządzenia ..
- (IBAction) grabImage: (id) nadawca { \t UIImagePickerController * picker = [[UIImagePickerController alloc] init]; \t picker.delegate = self; \t picker.allowsEditing = YES; \t picker.sourceType = UIImagePickerControllerSourceTypeCamera; \t [self presentModalViewController: selektor animowany: YES]; \t [wydanie picker]; } – nik
jeszcze jedno pytanie ... jak możemy przesłać obrazy/audio/wideo na serwer w iphone ..? Czy możesz podać przydatne linki ...? –
@kuldeep przekonwertować je do NSData, a następnie przesłać na serwer – KingofBliss
EDIT: 15 marca 2016 - Oto szybka wersja mojej wcześniejszej odpowiedzi, jeśli szukasz wersji objective-c będziesz znajdź to poniżej.
- SWIFT -
pierwsze zgodne z protokołem UIImagePickerControllerDelegate i protokół UINavigationControllerDelegate
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
uruchomić kompletacji obraz
func actionLaunchCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
else
{
let alert:UIAlertController = UIAlertController(title: "Camera Unavailable", message: "Unable to find a camera on this device", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
wdrożenie metod Delegat protokołu UIImagePickerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
// create a filepath with the current date/time as the image name
let savePath:String = self.documentsPath()! + "/" + self.presentDateTimeString() + ".png"
// try to get our edited image if there is one, as well as the original image
let editedImg:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage
let originalImg:UIImage? = info[UIImagePickerControllerOriginalImage] as? UIImage
// create our image data with the edited img if we have one, else use the original image
let imgData:NSData = editedImg == nil ? UIImagePNGRepresentation(editedImg!)! : UIImagePNGRepresentation(originalImg!)!
// write the image data to file
imgData.writeToFile(savePath, atomically: true)
// dismiss the picker
self.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
// picker cancelled, dismiss picker view controller
self.dismissViewControllerAnimated(true, completion: nil)
}
// added these methods simply for convenience/completeness
func documentsPath() ->String?
{
// fetch our paths
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if paths.count > 0
{
// return our docs directory path if we have one
let docsDir = paths[0]
return docsDir
}
return nil
}
func presentDateTimeString() ->String
{
// setup date formatter
let dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
// get current date
let now:NSDate = NSDate()
// generate date string from now
let theDateTime = dateFormatter.stringFromDate(now)
return theDateTime
}
- CEL-C -
EDYCJA: Aktualizacja, aby sprawdzić, czy kamera jest dostępna przed próbą uruchomienia. Dodano również kod pokazujący, jak zapisać zdjęcie png do folderu dokumentów w obszarze izolowanym aplikacji.
Spróbuj (zakłada się, że używasz ARC).
W pliku .h zgodne z protokołem Delegat:
@interface MyViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>
w pliku .m uruchomić próbnika obrazu (aparat fotograficzny):
-(void)actionLaunchAppCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
message:@"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
}
następnie wdrożyć protokoły delegat obsłużyć użytkownik anuluje wydarzenie lub zapisuje/edytuje/etc zdjęcie.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//This creates a filepath with the current date/time as the name to save the image
NSString *presentTimeStamp = [Utilities getPresentDateTime];
NSString *fileSavePath = [Utilities documentsPath:presentTimeStamp];
fileSavePath = [fileSavePath stringByAppendingString:@".png"];
//This checks to see if the image was edited, if it was it saves the edited version as a .png
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
//save the edited image
NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
[imgPngData writeToFile:fileSavePath atomically:YES];
}else{
//save the original image
NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage]);
[imgPngData writeToFile:fileSavePath atomically:YES];
}
[self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
również dodawać w EDIT: Oto metody użyte mu klasę Utilities uzyskania ścieżkę dokumentu oraz aktualną datę/czas
+(NSString *)documentsPath:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:fileName];
}
+(NSString *)getPresentDateTime{
NSDateFormatter *dateTimeFormat = [[NSDateFormatter alloc] init];
[dateTimeFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDateTime = [dateTimeFormat stringFromDate:now];
dateTimeFormat = nil;
now = nil;
return theDateTime;
}
// Otwartej Galerii
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:imagePickerController animated:YES completion:nil];
}];
}
else{
[self presentViewController:imagePickerController animated:YES completion:nil];
}
// Dla wersji otwartej Aparat
Oto zaktualizowana wersja odpowiedzi digitalHound, która działa dla Swift 3.
Działanie funkcji Uruchomienie aparatu:
func actionLaunchCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert:UIAlertController = UIAlertController(title: "Camera Unavailable", message: "Unable to find a camera on this device", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
alert.view.tintColor = UIColor(red:0.37, green:0.66, blue:0.44, alpha:1.0)
self.present(alert, animated: true, completion: nil)
}
}
Funkcje Delegat:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// create a filepath with the current date/time as the image name
let savePath:URL = URL(fileURLWithPath: self.documentsPath()! + "/" + self.presentDateTimeString() + ".png")
// try to get our edited image if there is one, as well as the original image
let editedImg:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage
let originalImg:UIImage? = info[UIImagePickerControllerOriginalImage] as? UIImage
// create our image data with the edited img if we have one, else use the original image
let imgData:Data = editedImg == nil ? UIImagePNGRepresentation(editedImg!)! : UIImagePNGRepresentation(originalImg!)! as Data
// write the image data to file
try! imgData.write(to: savePath, options: [])
// dismiss the picker
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// picker cancelled, dismiss picker view controller
self.dismiss(animated: true, completion: nil)
}
// added these methods simply for convenience/completeness
func documentsPath() ->String?
{
// fetch our paths
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
if paths.count > 0
{
// return our docs directory path if we have one
let docsDir = paths[0]
return docsDir
}
return nil
}
func presentDateTimeString() ->String
{
// setup date formatter
let dateFormatter:DateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
// get current date
let now:Date = Date()
// generate date string from now
let theDateTime = dateFormatter.string(from: now)
return theDateTime
}
To co pracował dla mnie.
Krok 1: Potwierdź UIImagePickerControllerDelegate , UINavigationControllerDelegate
Krok 2: (iOS 10+) Dodaj to jako klucz do pliku Info.plist Key: Bezpieczeństwo - kamera Wykorzystanie Opis
wartość: #Your wiadomość
Krok 3: i to w swoim @IBAction
if UIImagePickerController.isSourceTypeAvailable(.camera) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
- 1. iPhone App Store Pytanie
- 2. Menu LinkedIn iPhone app
- 3. Otwarcie pliku html offline programowo w Windows Phone 7
- 4. Symbolicate adhoc iphone app awarii
- 5. iPhone HTML5 App Przewijanie Pytanie
- 6. iPhone app build build warning
- 7. AdMob wywala iPhone App Xcode
- 8. Użyj czcionki niestandardowe w iPhone App
- 9. Otwarty klient poczty iPhone programowo
- 10. iPhone: Otwórz adres URL programowo
- 11. nagrywania wideo w iPhone SDK programowo
- 12. Jak programowo znaleźć pamięć w programie iPhone?
- 13. jak ukryć klawiaturę programowo w iphone
- 14. iPhone app Aktualizacja Vs nową wersję
- 15. IOS nowy Facebook iPhone App UISplitViewController Układ
- 16. Wordpress Blog iPhone Reader App Reader
- 17. iPhone Web App Number Pad z dziesiętnym
- 18. iPhone App OS Wykrywanie poprzez User Agent
- 19. iPhone app ulega awarii na WebThread MPVolumeSlider
- 20. Iphone 4.0 = jak odczytać SMS ze skrzynki odbiorczej IPhone Programowo
- 21. iPhone app awarii na iOS 4.0
- 22. Android: programowo kopiuje apk do/system/app
- 23. Ustawianie obrazu tła przycisku iPhone programowo
- 24. iphone - programowo przycisk powrotu kontrolera nawigacji
- 25. iPhone: rób zdjęcia z przednim aparatem programowo
- 26. UINavigationBar - Ustaw programowo? iPhone SDK 4
- 27. iphone - Programowo ustawione (systemowe) ustawienia proxy?
- 28. Jak programowo otworzyć wywołanie MKAnnotationView? (iPhone, MapKit)
- 29. wybierać kod USSD z iPhone programowo
- 30. IPhone/iPad: Jak programowo uzyskać szerokość ekranu?
jak otworzyć aparat w który ... można zakładać jakąś próbkę kod ... –
przeczytaj dokumentację, zawiera przykładowe kody – KingofBliss
jeszcze jedno pytanie ... jak możemy przesłać obrazy/audio/wideo na serwer w iphone ..? Czy możesz podać przydatne linki ...? –