Swift之本地相冊獲取圖片

最近做的項目中用到了從本地相冊獲取圖片,所以整理出來分享給大家 YoY

因為我們要用到相冊,所以我們要遵循兩個代理UIImagePickerControllerDelegate,UINavigationControllerDelegate,因為我用到了手勢點擊圖片,所以又遵循了UIGestureRecognizerDelegate,好啦,我們來看代碼

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate {

var imageView: UIImageView?
var imageUrl: NSURL?

override func viewDidLoad() {
    super.viewDidLoad()

   //創建一個imageView 將圖片設置成圓形
    imageView = UIImageView(frame: CGRectMake(100, 100, 80, 80))
    imageView?.userInteractionEnabled = true
    imageView?.backgroundColor = UIColor.cyanColor()
    imageView?.layer.masksToBounds = true
    imageView?.layer.cornerRadius = 40.0
    view.addSubview(imageView!)
    
    //添加手勢
    let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapGesture(_:)))
    tap.delegate = self
    tap.numberOfTapsRequired = 1
    tap.numberOfTouchesRequired = 1
    imageView!.addGestureRecognizer(tap)
}

//點擊頭像
func tapGesture(tap:UITapGestureRecognizer) {
    let aleat = UIAlertController(title: "照片選擇", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    weak var weakSelf = self;
    let aleartAction = UIAlertAction(title: "相冊", style: UIAlertActionStyle.Default) { (_) -> Void in
        
        self.imageVC.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        weakSelf!.presentViewController(self.imageVC, animated: true, completion: nil);
        
    }
    let aleartAction_two = UIAlertAction(title: "相機", style: UIAlertActionStyle.Default) { (_) -> Void in
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        {
            weakSelf!.imageVC.sourceType = UIImagePickerControllerSourceType.Camera
            weakSelf?.presentViewController(weakSelf!.imageVC, animated: true
                , completion: nil)
        }
    }
    let aleartAction_cancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (_) -> Void in
        
    }
    aleat.addAction(aleartAction);
    aleat.addAction(aleartAction_two);
    aleat.addAction(aleartAction_cancel);
    self.presentViewController(aleat, animated: true, completion: nil);
}

//懶加載創建imageVC
private lazy var imageVC:UIImagePickerController = {
    let imageVc = UIImagePickerController()
    imageVc.delegate = self
    return imageVc;
}()
// 選擇完成圖片后調用的方法
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    //獲取點擊圖片的url和image
    imageUrl = info["UIImagePickerControllerReferenceURL"] as? NSURL
    let image = info["UIImagePickerControllerOriginalImage"] as? UIImage
 
    let imageView = UIImageView()
    imageView.image = image
    imageView.frame = self.imageView!.frame
    imageView.layer.masksToBounds = true
    imageView.layer.cornerRadius = 40.0
    view.addSubview(imageView)
    
    dismissViewControllerAnimated(true, completion: nil)
    
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

好啦,今天就分享到這里咯,請大家多多指教,( _ )/~~拜拜

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

推薦閱讀更多精彩內容