iOS 相機、相冊調用

這是我自己寫的一個相機相冊調用分類。使用起來挺方便的。直接上代碼吧。

調用方法:

#import "UIViewController+ImagePickerAndCamera.h"

//在需要調用相機相冊的地方使用這個方法即可

[self createActionSheetForChooseImage:^(UIImage *image) {

//在這里處理圖片

}];

源碼:

#import@interface UIViewController (ImagePickerAndCamera)@property (nonatomic, copy) void (^CompleteBlock)(UIImage*);

// 創建彈出框來選擇圖片(相機相冊兩種方式)

- (void)createActionSheetForChooseImage:(void(^)(UIImage *))completeBlock;

@end


#import "UIViewController+ImagePickerAndCamera.h"#import#import "UIImage+UIImageExt.h"#import#importconst void * associateKey = @"ImagePickerAndCameraAssociateKey";

@implementation UIViewController (ImagePickerAndCamera)

- (void)setCompleteBlock:(void (^)(UIImage *))CompleteBlock{

objc_setAssociatedObject(self, associateKey, CompleteBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (void(^)(UIImage*))CompleteBlock{

return? objc_getAssociatedObject(self, associateKey);

}

// 創建彈出框來選擇圖片(相機相冊兩種方式)

- (void)createActionSheetForChooseImage:(void(^)(UIImage *))completeBlock{

UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"請選擇下列兩種方式的一種修改頭像" preferredStyle:UIAlertControllerStyleActionSheet];

__weak typeof(self) weakSelf = self;

[alertCtrl addAction:[UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[weakSelf localPhoto];

}]];

[alertCtrl addAction:[UIAlertAction actionWithTitle:@"拍攝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[weakSelf takePhoto];

}]];

[alertCtrl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

[self presentViewController:alertCtrl animated:YES completion:nil];

self.CompleteBlock = [completeBlock copy];

}

// 打開相機

- (void)takePhoto{

UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])

{

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if (authStatus == AVAuthorizationStatusAuthorized || authStatus == AVAuthorizationStatusNotDetermined) {

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

picker.delegate = self;

//設置拍照后的圖片可被編輯

picker.allowsEditing = YES;

picker.sourceType = sourceType;

[self presentViewController:picker animated:YES completion:nil];

}

else{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"請前往設置開啟拍攝權限" message:@"沒有拍攝權限,無法調用拍照功能" delegate:self cancelButtonTitle:@"不了" otherButtonTitles:@"好的", nil];

alert.tag = 10;

[alert show];

}

}

else

{

}

}

// 打開本地相冊

- (void)localPhoto{

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

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])

{

ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];

if (authStatus == ALAuthorizationStatusAuthorized? || authStatus == ALAuthorizationStatusNotDetermined) {

picker.delegate = self;

//設置選擇后的圖片可被編輯

picker.allowsEditing = YES;

[self presentViewController:picker animated:YES completion:nil];

}

else{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"請前往設置開啟相冊權限" message:@"沒有相冊權限,無法調用相冊功能" delegate:self cancelButtonTitle:@"不了" otherButtonTitles:@"好的", nil];

alert.tag = 10;

[alert show];

}

}

else{

}

}

//當選擇一張圖片后進入這里

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

//當選擇的類型是圖片

if ([type isEqualToString:@"public.image"])

{

//先把圖片轉成NSData

UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSData *data;

if (UIImageJPEGRepresentation(image,0.3) != nil)

{

data = UIImageJPEGRepresentation(image, 0.3);

}

else

{

data = UIImagePNGRepresentation(image);

}

image = [UIImage imageWithData:data];

UIImage *image1 = [image imageByScalingAndCroppingForSize:CGSizeMake(300, 300)];

//關閉相冊界面

[picker dismissViewControllerAnimated:YES completion:nil];

self.CompleteBlock(image1);

}

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

[picker dismissViewControllerAnimated:YES completion:nil];

}

@end


第一次寫簡書,別打那么慘號碼?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容