class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var clousureL: ((image: UIImage) -> Void)!
// var imageView: UIImageView!
var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
btn = UIButton(type: .Custom)
btn.frame = CGRect(x: 100, y: 100, width: 150, height: 150)
btn.setImage(UIImage(named: "image.png"), forState: .Normal)
//給button添加點擊事件
btn.addTarget(self, action: #selector(didButton(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
//給button添加單擊手勢
let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(_:)))
btn.addGestureRecognizer(tap)
}
func didButton(sender: UIButton){
didClick()
clousureL = {
(image) -> Void in
self.btn.setImage(image, forState: .Normal)
}
}
func tapClick(sender:UITapGestureRecognizer){
print("didclick")
didClick()
clousureL = {
self.btn.setImage($0, forState: .Normal)
}
}
func didClick(){
print("didclick")
let actionSheet = UIAlertController(title: "上傳頭像", message: nil, preferredStyle: .ActionSheet)
let cancelBtn = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
let takePhotos = UIAlertAction(title:"拍照",style: .Destructive,handler: {
(action: UIAlertAction) -> Void in
if UIImagePickerController.isSourceTypeAvailable(.Camera){
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
picker.allowsEditing = true
self.presentViewController(picker, animated: true, completion: nil)
}
else
{
print("模擬器中無法打開相機,請在真機中打開")
}
})
let selectPhotos = UIAlertAction(title:"相冊",style: .Default,handler: {
(action: UIAlertAction) -> Void in
let picker = UIImagePickerController()
picker.sourceType = .PhotoLibrary
picker.delegate = self
picker.allowsEditing = true
self.presentViewController(picker, animated: true, completion: nil)
})
actionSheet.addAction(cancelBtn)
actionSheet.addAction(takePhotos)
actionSheet.addAction(selectPhotos)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
clousureL(image: image!)
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
點擊獲取手機相冊圖片
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 1.先獲取到APP沙盒中的圖片路徑path2.然后將path作為參數,傳入下面的方法里,進行圖片保存到手機本地相冊中。
- 最近代碼君遇到一個問題,在其他手機調用系統相冊獲取圖片路徑都是可以的,但是華為手機,執行相同代碼,會報空指針異常,...
- 1.第一步在 - (BOOL)webView:(UIWebView *)webView shouldStartLo...
- 做APP基本上都是需要從系統的相冊當中獲取一張或多張圖片。那怎么做呢?下面我就帶你來實現這個內容,第一次寫。 我只...