How do you display an Uploaded (From Camera Roll) Image in Swift - swift

So I have just created my first app! It is almost finished! The only problem, I need to get a picture (uploaded from camera roll) into a Ui-Image! Please Help!
Also, I don't want to upload it to PHP or any other database languages. Just want it to stay.

You will need to export your Camera Roll image from iPhoto in a standard format, such as png. Here's an instruction page for exporting your photo.
Once you have your image in the correct format you can add it to your app's assets. In your Xcode Project Navigator, appears on left when not collapsed (look for a little folder icon), find Assets under your project file. Click on Assets, a new menu will fly out with a '+' at the bottom to add a group or image set. Follow the prompts to add your image.
Now that you can access the image from your application you may set it as the source file for a UIImage. Click on the image in your Storyboard. The Image attribute should now show your new photo in the drop down as an option. Select it. Alternatively, you may set the image attribute in your code. Something like this in Swift:
let myImage = UIImage(named: "yourImageName")!
imageView.image = myImage
Follow up to your comment about you image source. You can add a button that opens the user's album or camera. The higher level logic for onButtonClick looks something like this:
#IBAction func onButtonClick(sender: AnyObject) {
let actionSheet = UIAlertController(title: "Change Photo", message: nil, preferredStyle: .ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Album", style: .Default, handler: {action in self.showAlbum()}))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func showAlbum() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated:true, completion:nil)
}
Here are details for the UIImagePickerController()-- You will need a UIImagePickerControllerDelegate to finally set the image.

Related

Swift : OverlayView that appears at the bottom of the screen

I'd like to create a very common effect as in the picture below :
Explanation : the effect I'd like to accomplish consists in a view that appears (slides in) at the bottom of the screen when user clicks a button : you can still see the screen behind this view, but it applies a "dark layer" (black view with let's say 60% opacity) on the top of it. When user clicks on Block or Report absue (as for this example) it would perform the respective actions. Now when user clicks on cancel, and also when he clicks anywhere on the "dark layer" it would bring him back to the screen.
What I tried : presenting a new view controller (but it would use more data than necessary), using a overlay layer but I even didnt get close to that effect that we're usually seeing on apps. I'm not sure but I'd say that the best way to get that effect is using views ?
Does anyone have an idea please ?
Thanks and have a good day,
J
You are looking for a native element called UIActionSheet. It has become the part of UIAlertController, and does exactly what you are looking for.
Here is a little help how to set up one:
// Create you actionsheet - preferredStyle: .actionSheet
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create your actions - take a look at different style attributes
let reportAction = UIAlertAction(title: "Report abuse", style: .default) { (action) in
// observe it in the buttons block, what button has been pressed
print("didPress report abuse")
}
let blockAction = UIAlertAction(title: "Block", style: .destructive) { (action) in
print("didPress block")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("didPress cancel")
}
// Add the actions to your actionSheet
actionSheet.addAction(reportAction)
actionSheet.addAction(blockAction)
actionSheet.addAction(cancelAction)
// Present the controller
self.present(actionSheet, animated: true, completion: nil)
It should produce the following output:

Swift: Custom Nav Bar Image not firing action on click

I have a navigation bar on my viewcontroller.
I have created a custom image (of a cog) and have got that to show right:
//Add bar item
var image = UIImage(named: "settingsIcon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
NavigationBar.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: "addTapped")
I added the action to the above code:
action: "addTapped"
Then I created a function:
func addTapped() {
print("Tapped")
}
When I run the app, the image shows fine. When I click the image nothing happens, not even an error.
Am I doing something blatantly wrong here?
Try setting the target to self instead of nil.

Pick image from custom image library [Swift]

I am new to Swift/app development. I have successfully developed an app that uses an UIImagePickerController to let the user select an image from the device's photo library. However, I would like the user to select images from a custom set of images, and not from the device's photo library.
What would be the easiest way to implement this? Any help appreciated!
Edit
As per requested here is some of the code I am using.
I have a view controller that adheres to the following protocols:
class StockViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
On my main storyboard, I have an UIImageView with a tap gesturer which triggers an action on selectImageFromPhotoLibrary.
#IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
// Hide the keyboard.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary
imagePickerController.allowsEditing = false
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
presentViewController(imagePickerController, animated: true, completion: nil)
}
The only source types I can select for the imagePickerController are SavedPhotoAlbums, Camera or PhotoLibrary. I cannot select any app image folder.

How do I make a custom back button and action in SWIFT

I am trying to make a custom back button in swift although it never changes, I am slo trying to make the back button do an action and perform a function. The code where I am at:
var doneButton = UIImage(named: "arrow2.png")
var topLeftButton = UIBarButtonItem(image: doneButton, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("backToList"))
topLeftButton.tintColor = UIColor.whiteColor()
self.navigationItem.backBarButtonItem = topLeftButton //nothing happens
func backToList(){
saveData()// nothing runs
}
I want to be able to change the back button to my own UIImage and make a selector action happens once clicked.
thanks
You can add an UIImage instead of a button with the picture wanted and add a "Tap Gesture Recognizer" by dragging it to the UIImage. Also, don't forget to enable user interaction under the Attributes section of the image (see image below)
After that you control drag your UIImage into the code and create the action with your desired function.

iOS 8 showFromToolbar effect with UIActionController

UIActionController replaced UIActionSheet in iOS 8. Is there any way to recreate the showFromToolbar behavior of UIActionSheet in UIActionController? This is the effect I am seeking. Please note though that I am trying to draw a UIActionController coming out of an external action bar button on an iPhone, so I couldn't do this using generic UIActionSheet example too I suppose.
I created a UIBarButtonItem and, in its IBAction I am creating a UIActionController. I tried the following:
var alert = UIAlertController(title: "XYZ", message: "XYZ", preferredStyle: .Alert
// Then created a bunch of UIAlertAction items and added them to alert
var popover = alert.popoverPresentationController
if popover != nil {
var s = sender as UIBarButtonItem
popover?.sourceView = s.customView
popover?.sourceRect = s.customView!.bounds
popover?.permittedArrowDirections = .Any
} else {
println("POPOVER is nil")
}
self.presentViewController(alert, animated: true, completion: nil)
However, this doesn't work with either iPhone or iPad simulators - the alert is showing in middle of screen and the above code always prints "POPOVER is nil". Even if it would have worked in iPad it wouldn't have solved my original problem. Your advice is appreciated.
Found this wonderful tutorial that explains how to do it.