How to create front camera "flash" effect using the iPhone screen - ios

I'm trying to use iPhone 6s front camera's flash. I searched online and on Apple Docs but didn't find a useful explanation for how to use it. I'm currently making a custom front flash camera but I'm trying to use the official one used by Apple's camera.
Here's my code for my custom flash: (It's basically a white UIView that shows for a second and I set the phone's brightness to 1)
var flashview: UIView!
in viewDidLoad
flashview = UIView(frame: CGRectMake(UIScreen.mainScreen().bounds.width/2, UIScreen.mainScreen().bounds.height/2, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
flashview.center = CGPointMake(self.view.bounds.width/2, self.view.bounds.height/2)
flashview.backgroundColor = UIColor.whiteColor()
flashview.alpha = 0.0
flashview.layer.zPosition = 100
self.view.addSubview(flashview)
when photo is taken
UIScreen.mainScreen().brightness = CGFloat(1.0)
self.flashview.alpha = 1.0
sleep(1)
self.flashview.alpha = 0.9
sleep(1)
self.flashview.alpha = 0.0
UIScreen.mainScreen().brightness = CGFloat(0.5)
Is there any possible way to use the same flash used in the official Apple camera?

Any time you're tempted to use sleep(), slap yourself and realize you're Doing It Wrong™.
You really need to look into using animations. This uses nicely optimized (by the system) timings that won't block the main thread (the user interface among other things) by waiting for a forced sleep() to finish.
Your case is a little more complicated because I don't believe UIScreen's brightness is directly animatable. This means you'll need to create a custom animatable property (maybe call it "flashLevel" or something), which in turn updates UIScreen's brightness to match.
You should probably also note the user's brightness level first, animate to brightest second, then animate back to the user's level at the end of the "to maximum brightness" stage. I know it'd annoy me if an app screwed with my screen brightness and didn't put it back the way it was...
My best advice is to read the documentation to which I linked, try your best to get it working, then update your question to include the code you've tried and what's not working if you can't figure it out.

Related

How to use timing function for slowly speed up scaling function with SpriteKit

I have a sprite that I'm trying to scale out. I'm using the scaleTo skaction to do this... I want it to slowly ease in. And this was my initial solution:
let scal = SKAction.scale(by: 100, duration: 10)
scal.timingMode = SKActionTimingMode.easeIn
The problem is that as it scales out, since it is scaling out so much it appears to slow down. So I need to use the timeingFunction in order to write a custom easeIn for the action.
https://youtu.be/CE-B27gSXJI
In the video, you can see that it appears that it starts off fast and slows down. It only appears this way because I'm going a scale, and the bigger you go the slower it will appear...
Problem: I have no clue how to do this with the timing function, and I haven't been able to find a good source to use as a reference?
Any help would be appreciated and thank you!!!
Have a look at the Sprite Kit Utils from Ray Wenderlich for an example on how one can write an easing function: https://github.com/raywenderlich/SKTUtils Specifically, look at SKTTimingFunctions.swift and SKTEffects.swift.
There are some neat functions to give you better control of your easing. You might even want to use the easing functions that are defined there without changes, most of them work pretty well. For reference on how each easing function behaves, you can look at http://easings.net
Hope this helps!
I'm going to mark the answer above correct but this ended up being my solution and I wanted to share as it is simple to implement.
I don't fully understand how this works but when you specify the function for the timingFunction you have to include "t in". Then you can use this t to manipulate the timing of the animation.
So if you look at the links above, you can get the desired affect by using what is contained in the functions in Ray's SKTTimingFunctions.swift.
The below code shows how I used it; my timing function starts of slow and then slowly picks up speed... Again see the links above and you can get an idea.
let scal = SKAction.scale(by: 100, duration: 10)
scal.timingFunction = { t in
return t*t*t*t*t
}

Circular UI Slider with Image and Color

I am working on an app, with UISlider and need to make it circular.
Is it possible via some native code, or does a 3rd party library have to be integrated?
I want to fill color as user will slider over slider.
You can find 3rd party libraries for circular slider.
Take a look into these:
https://www.cocoacontrols.com/controls/uicircularslider
https://github.com/thomasfinch/Circular-UISlider
https://github.com/milianoo/CurvySlider
I came across this question in a seperate thread with multiple links and didn't like any of the answers offered so I thought I would add my own in what seemed to be the most relevant thread, on the off chance someone else finds their way here and likes the option I provide.
it is not elegant or simple but it is easy and I thought it would be a nice starting point for others to develop their own.
in this example the circular uiScroller is placed on a view (or HUD) that is presented over the top of an SKView. I then use a hitTest to transfer any touches from the gameScene to the UIView and use _touchesBegan and _touchesMoved to update a series of buttons that are hidden like so:
the arrows are all hidden but we use the frames of each box to register where the touch is
I made the arrows UIButtons so it was easier to use the storyboard but I think an imageView works fine as we are only checking the frame of the arrow inside _touchesBegan and then sending it to the function rather than collection a touch event from the arrow itself.
if upBtn.frame.contains(touch) {
upBtnPress() //if you made this a touch func like me a call to (self) as the sender may be required
}
the black circle itself remains visible so it looks like this is where your taps are going.
then finally you create multiple images like this:
any circular pattern for each location possible is appropriate
(I had a total of 16 points for mine)
and then under any press function you just change the image of the black circle to show the correct image based on where the touch location is and you're done!
circleDpad.image = UIImage(named: "up")
calculating the angle:
is quite very simple, there are 16 points on my dial so I need to convert this into a possible direction in 1...360
func angleChanged(angle: CGFloat) {
let valueReached = (angle / 16) * 360 // this gives you the angle in degrees, converting any point that's x/16 into x/360
let turret = checkBaseSelect() //this is my personal code
turret?.tAngle = CGFloat(valueReached) * (CGFloat.pi / 180) //this converts degrees to radians and applies it where I want it.
}
despite only having 16 points I found that even without applying animation it looks quite smooth. thanks for reading.
this is a gif of my circular slider in action
This is my first ever guide, if it needs more information (or should be somewhere else) please let me know.

How to lower brightness with UISlider so that it is lower than the lowest brightness setting available to the user?

I am making a clock application that is designed to be used to display the time at night. I want to find a way to make the brightness lower than what the user can make it, such as what is done in this app (https://itunes.apple.com/us/app/night-clock/id293660080?mt=8). I have tried UIScreen.mainScreen().brightness = CGFloat(0), but this only goes as dark as the user can make it. Any help?
Just dim all contents displayed by your app.
Instead of adjusting all colors you can get the same effect by adding an overlay view. Set the backgroundColor of this view to black with alpha 0.5, and then play with the alpha value.

Creating progress circle as WKInterfaceImage in Watch App

I am trying to create a progress circle for the Apple Watch version of my app. I know that we aren't able to use UIViews (which would make things so much easier!) so I am looking for alternatives.
Basically, I would like to create one of these prototypes:
The way I was hoping to do things was to add the background layers as a normal WKInterfaceImage and then the progress arrow/line on top as a WKInterfaceImage that rotates around the circle based on the percentage calculated.
I have the percentage calculated so basically, what I am looking for is some help with the math code for rotating the arrow.
Does anyone know if this is possible, and could anyone help me out if so? I'm not trying to update the circle while the app is running; it just needs to update when the Watch App launches to correspond with the iOS version.
Thanks!
As of watchOS 3 it is possible to use SpriteKit.
SKShapeNode can draw shapes, so it is possible to create radial rings.
Add a WKInterfaceSKScene to your interface controller in storyboard and hook it up through an outlet.
Set it up in the awake method of the interface controller
override func awake(withContext context: Any?) {
super.awake(withContext: context)
scene = SKScene(size: CGSize(width: 100, height: 100))
scene.scaleMode = .aspectFit
interfaceScene.presentScene(scene)
}
Create a shape node and add it to the scene
let fraction: CGFloat = 0.75
let path = UIBezierPath(arcCenter: .zero,
radius: 50,
startAngle: 0,
endAngle: 2 * .pi * fraction,
clockwise: true).cgPath
let shapeNode = SKShapeNode(path: path)
shapeNode.strokeColor = .blue
shapeNode.fillColor = .clear
shapeNode.lineWidth = 4
shapeNode.lineCap = .round
shapeNode.position = CGPoint(x: scene.size.width / 2, y: scene.size.height / 2)
scene.addChild(shapeNode)
Another solution is to create 100 picture, for each number you have a frame. So, doing that, you can show an animation using 'startAnimatingWithImagesInRange:duration:repeatCount' method.
The problem is that it's hard to customise each frame. Somebody thought about this issue and created a generator. You can find it by this name:
Radial Bar Chart Generator
This link should help you to customise the frames: http://hmaidasani.github.io/RadialChartImageGenerator/
Also you have same samples on git link.
For 100 frames with a single arc frame you get around 1,8 MB on disk.
Most of what is available on iOS is not present (yet) in WatchKit. In particular, several of the things you want to do are almost impossible. (Glimmer of hope in a moment). In particular, you cannot rotate an image. Or rather, you can rotate an image, but you have to do it on the phone and then pass that image up to the watch at runtime. Also, you cannot easily composite images - however, there is a way to do it.
One way would be to construct the entire rotated, composited image the way you want it on the phone and pass the final data up to the button using [WKInterfaceButton setBackgroundImage:]. Unfortunately, you will likely find this to be slow in the simulator, and most likely it will work poorly on the actual watch. Hard to know for sure because we don't have one, but this is sending the image on the fly over Bluetooth. So you won't get smooth animation or good response times.
A better way is to hack your way to it on the watch. This relies on two tricks: one, layering groups together with background images. Two, using -[WKInterfaceImage startAnimatingWithImagesInRange:duration:repeatCount:].
For the first trick, drop a Group into your layout, then put another group inside it, then (possibly) a button inside that. Then use -[WKInterfaceGroup setBackgroundImage:] and the images will composite together. Make sure you use proper transparency, etc.
For the second trick, refer to the official documentation - essentially, you will need a series of images, one for each possible rotation value, as erdekhayser said. Now, this may seem egregious (it is) and possibly impractical (it is not). This is actually how Apple recommends creating spinners and the like - at least for now. And, yes, that may mean generating 360 different images, although because of the small screen, my advice is to go every 3-5 degrees or so (nobody will be able to tell the difference).
Hope this helps.
Nobody posts code??? Here it is! enjoy:
1) Create the images here: http://hmaidasani.github.io/RadialChartImageGenerator/
2) Drag n Drop a picker into your View Controller, and link it to some viewController.swift file. Set the Picker style to "Sequence" on the menu that appears on the right.
3) Add this code to the viewController.swift , and connect the picker to the IBOutlet and the IBAction:
import WatchKit
import Foundation
class PickerViewController: WKInterfaceController {
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
}
#IBOutlet var itemPicker: WKInterfacePicker!
override func willActivate() {
super.willActivate()
let pickerItems: [WKPickerItem] = (0...100).map {
let pickerItem = WKPickerItem()
pickerItem.contentImage = WKImage(imageName: "singleArc\($0).png")
return pickerItem
}
itemPicker.setItems(pickerItems)
}
#IBAction func pickerSelectedItemChanged(value: Int) {
NSLog("Sequence Picker: \(value) selected.")
}
override func didDeactivate() {
super.didDeactivate()
}
}
WKInterface classes are not able to be subclassed. Therefore, a custom control is not possible.
Also, animation is limited. In order to create an animation, you must store every single frame as an image. Then, you can have an image view in your WatchKit app that cycles through these images.
Store the images in the Images.xcassets folder in the watch target, and try to mess around with the changing the frame based on the percentage the activity is finished.
One extra note: having 100 images would not be efficient, as each WatchKit app has only a limited amount of space on the watch it can take up (I believe it is 20MB, but I am not sure). Maybe have an image for every 5%.
No, there is not possible to creating this custom circle on watch kit, because UIView doesn't work on watch kit.
there is only solution of your problem is you have to put 100 images of each frames... and make sure that size of images is lesser than 20 MB. because the size of watch application is up to 20 MB

Take a picture, choose between Full or Square Mode in camera

Is there a way to use the overlays in the UIImagePickerController to show the square picture a user might use, while having a toggle button in there somewhere to switch on the fly?
Currently the iOS 7 camera has this capability, but UIImagePickerController does not (thanks Apple), so is there a way to add this functionality?
Would using AVCaptureSession be necessary? It seems like serious overkill, and I'd have to program flash/focus/etc all over again, I think.
Failing that, is there a customizable class that already exists that I could just implement?
I'm banging my head against the wall trying to figure out the best course of action here, so any help would be appreciated.
You can adjust the camera view by transforming the scale
CGAffineTransform transform = CGAffineTransformMakeScale(1.0, 0.5);
imagePickerController.cameraViewTransform = transform;
with the arguments being the scale to change the x and y axis of the camera screen by. You could add a button that re-presents the camera view within the view controller at the top of your hierarchy. It may not perfectly recapitulate the native phone app, but it will be a pretty good approximation.
I would probably do this with a custom cameraOverlayView that letterboxes the viewfinder. Once the picture has been taken you'll have to crop the resulting image.

Resources