IOS-simulyatorida ishlayotganda, AppPicker-ning manbai Kamera bo'lsa App tushadi?

Mening dasturim simulyatorda qulab tushadi, lekin men tasvir pikerining kamera turidagi turini berganimda qurilmada yaxshi ishlaydi. Men "plist" da kamera va fotosuratga ruxsatnoma qo'shganman.

Crash log:

Istisno qilinmaganligi sababli dasturni bekor qilish   'NSInvalidArgumentException', sabab: 'Manba turi 1 mavjud emas'

Kod:

-(void)showImgPickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}

Kamera simulyatorga kirishga harakat qilayotganimdan keyin buzilish sodir bo'layaptimi? bu erda nima bo'ladi. Men sog'indimmi?

2
Qutqarish hisobotini ko'rsata olasizmi? Shunday qilib, muammoingizni bilib olamiz.
qo'shib qo'ydi muallif Nirmalsinh, manba
Iltimos, bizga kassetani ko'rsata olasizmi?
qo'shib qo'ydi muallif iPeter, manba
Chiqish hisobotida Resurs turi 1 mavjud emas. Manba turi 1 Camera degan ma'noni anglatadi. Simulyatordan simulyatordan foydalana olmaysiz, shuning uchun simulyatorda kamera yo'qligi sababli, simulyatordan ilovaga ishdan tushib qolganda kameraga kirishga urinayotganingizda.
qo'shib qo'ydi muallif iPeter, manba
Kamera kameraga simulyatordan kira olmaysiz, shuning uchun siz qurilmada kamera mavjudligini tekshirishingiz kerakmi yoki buzilishdan qochish uchun emas
qo'shib qo'ydi muallif Abdullah Saeed, manba
*** 'NSInvalidArgumentException', sababi: 'Manba turi 1 mavjud emas'
qo'shib qo'ydi muallif fAiSaL, manba

6 javoblar

Berilgan manba turining mavjud yoki yo'qligini tekshirish uchun isSourceTypeAvailable usulini ishlatishingiz mumkin UIImagePickerController .

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType

Qurilma belgilangan manba turini qo'llab-quvvatlasa Ha ni qaytaradi; Agar yo'q bo'lsa   ko'rsatilgan manba turi mavjud emas.

     

Media manbai mavjud bo'lmasligi yoki mavjud bo'lmasligi sababli,   qurilmalar har doim manba turlarini qo'llab-quvvatlamasligi mumkin. Masalan, agar siz   foydalanuvchi kutubxonasidan rasmni olishga urinish va kutubxona   bo'sh bo'lsa, ushbu usul NO yozuvini qaytaradi. Xuddi shunday, agar kamera allaqachon mavjud bo'lsa   Bu usulni NO ishlatadi.

     

UIImagePickerController ob'ektini tasvirni tanlash uchun ishlatishdan oldin, ushbu usulni siz kerakli manba   turi mavjud.

Mavjud satrlarni tekshirish uchun quyidagi kodni ishlatish mumkin:

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = sourceType;
    picker.allowsEditing = YES;
    [(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];     

}
else{

    //it is simulator or device with no camera source

   //handle accordingly

}

Reference: https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619144-issourcetypeavailable?language=objc

2
qo'shib qo'ydi

Tez ishlayotgan bo'lsangiz, ushbu kodni kiriting.

if(UIImagePickerController.isSourceTypeAvailable(.camera))
{
         let myPickerController = UIImagePickerController()
         myPickerController.delegate = self
         myPickerController.allowsEditing = true
         myPickerController.sourceType = .camera
         self.present(myPickerController, animated: true, completion: nil)
}
else
{
         let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .alert)
         let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel) { action -> Void  in
                    //Just dismiss the action sheet
         }
         actionController.addAction(cancelAction)
         self.present(actionController, animated: true, completion: nil)
}
1
qo'shib qo'ydi

simulyatorda kameraga kira olmaysiz, shuning uchun kamera mavjud yoki yo'qligini tekshirishingiz kerak

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// do your stuff
} else {
//camera not available
}
1
qo'shib qo'ydi

"NSInvalidArgumentException" sababi tufayli bekor qilinadigan dasturni bekor qilish sababi: "Manba turi 1 mavjud emas" - bu kameraga kirishning imkoni bo'lmagan simulyatorda appning ishdan chiqishiga sabab bo'ladi. Kameraga faqat qurilmaga kiring, shuning uchun quyidagi kodni kiriting bir falokat oldini olish.

-(void)showImgPickerWithSourceType:    (UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = sourceType;
}
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
1
qo'shib qo'ydi

Kamerani simulyatorga ochgansiz va, albatta, simulyatorda kamera yo'q ... Shu kabi ogohlantirishni davom eting,

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                      message:@"Device has no camera"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles: nil];

[myAlertView show];

}
else{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = sourceType;
    picker.allowsEditing = YES;
    [(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}

Kamera noto'g'ri qurilma qurilmasidagi falokatlarning oldini olish uchun ushbu kodni har doim qo'shishga yordam berishi mumkin va umid qilsangiz ...

1
qo'shib qo'ydi
UIAlertView ishlatilmaydi, buning o'rniga UIAlertController dan foydalaning.
qo'shib qo'ydi muallif Ronak Chaniyara, manba
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
               UIImagePickerController *imagePickerController=[[UIImagePickerController alloc] init];
               imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
               imagePickerController.allowsEditing = YES;
               imagePickerController.delegate = self;
              [self presentViewController:imagePickerController animated:YES completion:^{}];
 }
else
{
    //Camera not available
}
1
qo'shib qo'ydi