[Swift in Xcode]Take photo or select image from album

2016-03-01

March is coming =w=..This month, still updating some tips and tutorials for swift. In this article, I am going to talk about how to use camera in swift. Basically, I will divide that into two categories:

  • Take a photo
  • Select a image from album
  • Get the image we want

In order to us the imagePicker, first, we need to let our viewController inherited with the UIImagePickerControllerDelegate and UINavigationControllerDelegate, do not forget the UINavigationControllerDelegate.

Take a photo

The following code is for open the camera:

1
2
3
4
5
6
7
8
9
10
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let cameraViewController = UIImagePickerController()
cameraViewController.sourceType = UIImagePickerControllerSourceType.Camera
cameraViewController.delegate = self
self.presentViewController(cameraViewController, animated: true, completion: nil)
print("Tapped wow")
}
else{
print("Disable tapped")
}

However, when we take our image how to store that image. It will be discussed after I display the code for get a image from album.

Select a image from album

The following code is for open the album:

1
2
3
4
5
let picker : UIImagePickerController = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker.delegate = self
picker.allowsEditing = false
self.presentViewController(picker, animated: true, completion: nil)

Now, we have got the image, and we may need to upload this image(will be discussed in the following posts, please check my blog often) to our server or display that image.

Get the image we want

The following code is for didFinishPickingImage, the argument image is the image we get, in this example, I replace the userIcon(UIImageViewController)’s image with the image I just get and store our image into the coreData. The image for the coreData is a NSData so do not forget to write UIImageJPEGRepresentation, I find that the picture iPhone take is jpeg.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.userIcon.image = UIImage(named: "parentIcon")
// let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let person = self.loguser![0] as NSManagedObject
person.setValue(UIImageJPEGRepresentation(image, 1), forKey: "userImage")
do{
self.userIcon.image = image
print("Successfully updated photo")
try person.managedObjectContext?.save()
print("Insert into coreData")
self.myImageUploadRequest()
}catch _{
}
picker.dismissViewControllerAnimated(true, completion: nil)
}

Also, here is one more tip, that how to convert a imageURL to the UIImage, the code is following:

1
2
3
4
5
6
7
8
9
func getProfPic(imgURLString: String) -> UIImage? {
let imgURL = NSURL(string: imgURLString)
let imageData = NSData(contentsOfURL: imgURL!)
if(imageData != nil){
let image = UIImage(data: imageData!)
return image
}
}

For the next following post, I will update some article about how to upload Image to the server. Please check.


Comments: