반응형
objectiv c
info.plist
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Message requesting the ability to add to the photo library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>
ImagePickerManager.h
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
@interface ImagePickerManager : NSObject<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
+ (ImagePickerManager*)store;
-(void)getGallery:(UIViewController*)viewcontroller
pickImageHandler:(void (^)(UIImage*))pickImageHandler
cancelHandler:(void (^)(void))cancelHandler;
-(void)getCamera:(UIViewController*)viewcontroller
pickImageHandler:(void (^)(UIImage*))pickImageHandler
cancelHandler:(void (^)(void))cancelHandler;
@end
ImagePickerManager.m
#import "ImagePickerManager.h"
static ImagePickerManager *_store;
@interface ImagePickerManager(){
UIImagePickerController *picker;
void (^pickImageHandler)(UIImage*);//이미지 콜백
void (^cancelHandler)(void);//취소 콜백
}
@end
@implementation ImagePickerManager
+ (ImagePickerManager *) store{
if (!_store){
_store = [[ImagePickerManager alloc] init];
}
return _store;
}
- (instancetype)init
{
self = [super init];
if (self) {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
}
return self;
}
//앨범에서 가저오기
-(void)getGallery:(UIViewController*)viewcontroller pickImageHandler:(void (^)(UIImage*))pickImageHandler cancelHandler:(void (^)(void))cancelHandler {
self->pickImageHandler = pickImageHandler;
self->cancelHandler = cancelHandler;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[viewcontroller presentViewController:picker animated:true completion:nil];
}
//카메라에서 가저오기
-(void)getCamera:(UIViewController*)viewcontroller pickImageHandler:(void (^)(UIImage*))pickImageHandler cancelHandler:(void (^)(void))cancelHandler {
self->pickImageHandler = pickImageHandler;
self->cancelHandler = cancelHandler;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[viewcontroller presentViewController:picker animated:true completion:nil];
}
#pragma mark - imagePicker extenstion
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
if (cancelHandler){
dispatch_async(dispatch_get_main_queue(), ^{
self->cancelHandler();
});
}
[picker dismissViewControllerAnimated:true completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
UIImage *image =(UIImage*)info[UIImagePickerControllerOriginalImage];
if (pickImageHandler){
dispatch_async(dispatch_get_main_queue(), ^{
self->pickImageHandler(image);
});
}
[picker dismissViewControllerAnimated:true completion:nil];
}
@end
사용 방법
//카메라
-(void)getCamera{
[ImagePickerManager.store getCamera:self pickImageHandler:^(UIImage * image) {
//get image
} cancelHandler:^{
//user cancel
}];
}
//앨범
-(void)getAlbum{
[ImagePickerManager.store getGallery:self pickImageHandler:^(UIImage * image) {
//get image
} cancelHandler:^{
//user cancel
}];
}
swift
ImagePickerManager.class
import UIKit
class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker = UIImagePickerController();
var alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
var viewController: UIViewController?
var pickImageCallback : ((UIImage) -> ())?;
var pickImageCancelCallback : (() -> ())?;
override init(){
super.init()
}
func pickImage(_ viewController: UIViewController, _ callback: @escaping ((UIImage) -> ())) {
pickImageCallback = callback;
self.viewController = viewController;
let cameraAction = UIAlertAction(title: "Camera", style: .default){
UIAlertAction in
self.openCamera()
}
let galleryAction = UIAlertAction(title: "Gallery", style: .default){
UIAlertAction in
self.openGallery()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel){
UIAlertAction in
}
// Add the actions
picker.delegate = self
alert.addAction(cameraAction)
alert.addAction(galleryAction)
alert.addAction(cancelAction)
alert.popoverPresentationController?.sourceView = self.viewController!.view
viewController.present(alert, animated: true, completion: nil)
}
func pickImage(_ viewController: UIViewController, _ callback: @escaping ((UIImage) -> ()), _ cancelCallback:@escaping ()->()) {
pickImageCallback = callback;
pickImageCancelCallback = cancelCallback;
self.viewController = viewController;
let cameraAction = UIAlertAction(title: "Camera", style: .default){
UIAlertAction in
self.openCamera()
}
let galleryAction = UIAlertAction(title: "Gallery", style: .default){
UIAlertAction in
self.openGallery()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel){
UIAlertAction in
}
// Add the actions
picker.delegate = self
alert.addAction(cameraAction)
alert.addAction(galleryAction)
alert.addAction(cancelAction)
alert.popoverPresentationController?.sourceView = self.viewController!.view
viewController.present(alert, animated: true, completion: nil)
}
func openCamera(){
alert.dismiss(animated: true, completion: nil)
if(UIImagePickerController.isSourceTypeAvailable(.camera)){
picker.sourceType = .camera
self.viewController!.present(picker, animated: true, completion: nil)
} else {
let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
alertWarning.show()
}
}
func openGallery(){
alert.dismiss(animated: true, completion: nil)
picker.sourceType = .photoLibrary
self.viewController!.present(picker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
pickImageCancelCallback!()
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
pickImageCallback?(image)
}
@objc func imagePickerController(_ picker: UIImagePickerController, pickedImage: UIImage?) {
}
}
사용법
ImagePickerManager().pickImage(self, { img in
//get image
}) {
//user cancel
}
결론 : swift가 짱이다..
반응형
'IOS' 카테고리의 다른 글
(IOS) IDFV ( Identifier For Vendor ) (0) | 2023.01.03 |
---|---|
(ios)(에러) duplicate symbol for architecture arm64(static library) (0) | 2020.09.02 |
(ios)에러 - content and frame layout guides before 11.0 (0) | 2020.03.31 |
(IOS)인앱 정기결제 적용하기(수정중) (1) | 2020.03.02 |
(IOS)WKWebview 앱캐시 문제 (1) | 2020.02.26 |