你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 相機調用

iOS 相機調用

編輯:IOS開發綜合

調用相機需要引入兩個代理

UIImagePickerControllerDelegate,UINavigationControllerDelegate


否則出出現:

設置delegate時候,出現警告:Assigning to ‘id’ from incompatible type ‘AddSightingViewController *const __strong’

解決過程】

1.問題很明顯是 strong不兼容,然後就去試試,把相應的AddSightingViewController改一下,從

@property (nonatomic) UIImagePickerController *imgPickerController;

改為:

@property (nonatomic, weak) UIImagePickerController *imgPickerController;

然後就消除了此警告了。

注:有時候Xcode不能及時消除警告和錯誤,需要手動去Product->Clean一下。

【總結】

好像Property變量,如果不指定引用類型,則默認是strong引用,此處改為weak,即可解除此警告。

而另外關於weak,strong等更詳細的含義,有空再整理。


//相機


-(void)takePhotoAction:(UIButton *)sender{

FZLog(nil, nil);

//先設定sourceType為相機,然後判斷相機是否可用(ipod)沒相機,不可用將sourceType設定為相片庫

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化

picker.delegate = self;

picker.allowsEditing = YES;//設置可編輯

/*sourceType = UIImagePickerControllerSourceTypeCamera; //照相機

sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //圖片庫

sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片

*/

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:nil];//進入照相界面


}

}

// 打開相冊:(區分pad和iphone)

-(void)openPhotosAction:(UIButton *)sender{

// for iphone:

UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//pickerImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];

}

pickerImage.delegate = self;

pickerImage.allowsEditing = NO;

[self presentViewController:pickerImage animated:YES completion:nil];//進入照相界面

}


//for ipad:

-(void)ipadPicker{

UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; //保存的相片

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.allowsEditing = NO;//是否允許編輯

picker.sourceType = sourceType;

/*

如果從一個導航按鈕處呈現,使用:

presentPopoverFromBarButtonItem:permittedArrowDirections:animated:;

如果要從一個視圖出呈現,使用:

presentPopoverFromRect:inView:permittedArrowDirections:animated:

如果設備旋轉以後,位置定位錯誤需要在父視圖控制器的下面方法裡面重新定位:

didRotateFromInterfaceOrientation:(在這個方法體裡面重新設置rect)

然後再次調用:

- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated

*/

//UIPopoverController只能在ipad設備上面使用;作用是用於顯示臨時內容,特點是總是顯示在當前視圖最前端,當單擊界面的其他地方時自動消失。

UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker];

//permittedArrowDirections 設置箭頭方向

[popover presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}



調用相機自定義《中文顯示,相機全屏》

中文相機

在Iphone利用UIImagePickerController調用系統照相機一文中介紹了怎樣調用系統照相機。但是有時候我需要自定義相機內容,需要以相機內容為背景,在其視圖上添加其他控件,下面是相機試圖最大化的實例。


-(void)onClickbutton:(id)sender

{

UIImagePickerController *controller = [[UIImagePickerController alloc] init];

[controller setSourceType:UIImagePickerControllerSourceTypeCamera];

[controller setShowsCameraControls:NO];

CGAffineTransform cameraTransform = CGAffineTransformMakeScale(1.25,1.25);

controller.cameraViewTransform = cameraTransform;

[self presentViewController:controller animated:YES completion:nil];//進入照相界面

}

targets->custom iOS target properties (Localization native development region設為China)



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved