2017년 2월 20일 월요일

UIImagePickerController 를 이용한 Photo Library 사진 로드 @@ in Swift3 - Xcode 8.2 iOS 10

!!중요
info.plist 에서 Privacy - Photo Library Usage Description  키값을 추가하여 Value 값에 글을 넣어야 합니다. 이작업 없이 진행할 경우 무조건 디버깅중에 아래 글 뜨면서 오류 발생


The app's Info.plist must ... key with a string value explaining to the user how the app uses this data.

<참조>
https://swifteyes.blogspot.kr/2016/11/photolibrary-in-swift3-xcode-80-ios-10.html


1. Delegate 추가
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

2. imageView, View  아울레 추가
 //불러온 사진이 들아갈 뷰
@IBOutlet weak var profileImage: UIImageView!
// 터치하여 Photo Library 를 호출
@IBOutlet weak var profileView: UIView
// UIImagePickerController
let ProfileimagePicker = UIImagePickerController()


3. viewDidLoad() 에 Photo Library 를 호출할 뷰에 터치 액션 넣기
override func viewDidLoad() {
  super.viewDidLoad()

// profileView 를 터치하면  photoAddAction() 가 호출된다
let photoAddAction = UITapGestureRecognizer(target: self, action: #selector(self.photoAddAction(_:)))
self.profileView.addGestureRecognizer(photoAddAction)


4. photoAddAction(_ sender: AnyObject) 에 코드 넣기
func photoAddAction(_ sender: AnyObject){
//포토라이브러리 지원여부
  if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){

    //에디팅 여부 true 경우 사진을 에디팅 할 수 있는 뷰 나타남, 에디팅 없이 사진 불러오거나  자신이 만든 에디팅뷰 사용할 경우 false로 설정
    ProfileimagePicker.allowsEditing = true
    // camera, Save photoalbum, PhotoLibrary 중 타입 선택

    ProfileimagePicker.sourceType = .photoLibrary
  self.present(ProfileimagePicker, animated: true, completion: nil)
  }else{
    //"사진라이브러리 접근 할 수 없음" 알럿 뷰
    let alertController = UIAlertController(title: "test", message:
    "Can not access the photo library", preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "Check", style: UIAlertActionStyle.default,handler: nil))
    self.present(alertController, animated: true, completion: nil)
  }
}


4. 에디팅 된 사진 가져오기
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  var newImage: UIImage
  if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
    newImage = possibleImage
  } else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
    newImage = possibleImage
  } else {
    return
  }
//에디팅 후 가져온 사진 이미지뷰에 넣기
  profileImage.image = newImage
  self.dismiss(animated: true, completion: nil)
/*
자신이 만든 에디팅 뷰로 사진을 가져가고 싶다면 아래코드 참조
// 자신이 만든 에디팅 뷰컨트롤러 로드
let uvc = self.storyboard!.instantiateViewController(withIdentifier: "CustomimageView") as! CustomPhotoViewController 
// 뷰넘어가는 퍼포먼스 (없어도됨)
uvc.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
// 커스텀 뷰 컨트롤러의 이미지 뷰에 사진 넣기
uvc.Cropimage = newImage 
self.present(uvc, animated: true, completion: nil)
*/
}

// 포토라이브러리 Cancel 버튼 클릭 시
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  self.dismiss(animated: true, completion: nil)
}

2017년 2월 15일 수요일

CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero Swift3문법으로 사용 @@ in Swift3 - Xcode 8.2 iOS 10

Swift2 에서 사용되던 CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero가 Swift3에선 바뀌었다.



CGRect(x: Int, y: Int, width: Int, height: Int
CGVector(dx: Int, dy: Int
CGPoint(x: Double, y: Double
CGSize(width: Int, height: Int)


Example:

let newPoint = stackMid.convert(CGPoint(x: -10, y: 10), to: self)
self.physicsWorld.gravity = CGVector(dx: 0, dy: gravity)
hero.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 16, height: 18))
let back = SKShapeNode(rect: CGRect(x: 0-120, y: 1024-200-30, width: 240, height: 140), cornerRadius: 20)


더 자세한 내용은 아래 참조

2017년 2월 7일 화요일

UIButton.addTarget 사용 @@ in Swift3 - Xcode 8.2 iOS 10

UIButton.addTarget 사용하기

button.addTarget(self, action:#selector(handle(sender:)), for: .touchUpInside)


func handle(sender: UIButton){ 

  //... 
}

추천 게시물

애플 개발자 등록방법 2016년 5월 8일 기준!!

애플 개발자 등록 절차 1. 개발자 등록 페이지 이동    애플 개발자 로그인 > Account 페이지 이동 > 하단 영역 클릭 (이미지 참조)   >> Enroll 클릭 >> 무조건 승인!! ...