NEW! Master Swift design patterns with my latest book! >>

Media

Found 24 articles in the Swift Knowledge Base for this category:

 

CIDetectorTypeFace: How to detect faces in a UIImage

Core Image has a number of feature detectors built right in, including the ability to detect faces, eyes, mouths, smiles and even blinking in pictures. When you ask it to look for faces in a picture, it will return you an array of all the faces it found, with each one containing face feature details such as eye position. Here's an example:... Continue Reading >>

How to choose a photo from the camera roll using UIImagePickerController

The UIImagePickerController class is a super-simple way to select and import user photos into your app. As a bonus, it also automatically handles requesting user permission to read the photo library, so all you need to do is be ready to respond when the user selects a photo.... Continue Reading >>

How to control the pitch and speed of audio using AVAudioEngine

Although it’s easy enough to play sound effects and music using AVKit, it’s actually one of the most powerful frameworks in iOS and can easily add some fun and interesting effects to your apps and games.... Continue Reading >>

How to convert text to speech using AVSpeechSynthesizer, AVSpeechUtterance and AVSpeechSynthesisVoice

If you're looking for text-to-speech conversion, it's baked right into iOS thanks to the AVSpeechSynthesizer class and its friends. As you can tell from the "AV" part of its name, you'll need to add AVFoundation to your project, like this:... Continue Reading >>

How to create a barcode

You can generate a string into a traditional barcode using iOS using Core Image, but you should make sure and convert your input string to a Data using String.Encoding.ascii to ensure compatibility. Here's a function you can use that wraps it all up neatly, including scaling up the barcode so it's a bit bigger:... Continue Reading >>

How to create a PDF417 barcode

PDF417 barcodes - most frequently seen on boarding passes at airports, but also seen in digital postage stamps and other places – are built right into iOS. This function below accepts a string as its only parameter and returns a UIImage containing the PDF417 barcode representing that string:... Continue Reading >>

How to create a QR code

iOS has a built-in QR code generator, but it's a bit tricksy to use because it's exposed as a Core Image filter that needs various settings to be applied. Also, it generates codes where every bit is just one pixel across, which looks terrible if you try to stretch it inside an image view.... Continue Reading >>

How to desaturate an image to make it black and white

One of the most useful filters to have in your toolbox is called CIColorMonochrome, and its job is to remove the color variance from an image then tint it however you want. If you use gray as your tint color, it produces plain black and white images, but you can also use other colors to get sepia tone and other effects.... Continue Reading >>

How to filter images using Core Image and CIFilter

Core Image is the one of the most powerful frameworks available to iOS developers: it makes hardware-accelerated image manipulation ridiculously easy, which means you get to add powerful graphical effects to your apps and games with very little work.... Continue Reading >>

How to highlight text to speech words being read using AVSpeechSynthesizer

iOS has text-to-speech synthesis built right into the system, but even better is that it allows you to track when individual words are being spoken so that you can highlight the words on the screen. This is extremely easy to do thanks to the AVSpeechSynthesizerDelegate protocol: you get two callbacks in the form of willSpeakRangeOfSpeechString and didFinish, where you can do your work.... Continue Reading >>

How to loop audio using AVAudioPlayer and numberOfLoops

By default AVAudioPlayer plays its audio from start to finish then stops, but you can control how many times to make it loop by setting its numberOfLoops property. For example, to make your audio play five times in total, you’d write this:... Continue Reading >>

How to make resizable images using resizableImage(withCapInsets:)

If you use a small image in a large image view, you can make the image stretch to fit if you want to but it probably won't look great. iOS provides an alternative known as resizable images, which is where you define part of an image as being fixed in size and let iOS stretch the remainder.... Continue Reading >>

How to pixellate a UIImage

Core Image has a number of interesting filters baked in, and an easy one to use is CIPixellate – it pixellates images, making them appear blocky. You have control over how big each pixel block should be, so it’s suitable for a range of tasks.... Continue Reading >>

How to play sounds using AVAudioPlayer

The most common way to play a sound on iOS is using AVAudioPlayer, and it's popular for a reason: it's easy to use, you can stop it whenever you want, and you can adjust its volume as often as you need. The only real catch is that you must store your player as a property or other variable that won't get destroyed straight away – if you don't, the sound will stop immediately.... Continue Reading >>

How to play videos using AVPlayerViewController

AVPlayerViewController is designed to make it easy to play media just like Apple’s own apps. You can play movies and audio, local and remote, and you benefit from the same user interface layout used elsewhere in the system.... Continue Reading >>

How to read the average color of a UIImage using CIAreaAverage

Core Image has a filter that resample an image down to 1x1 pixels so you can read the average color, although it’s a weirdly hard to use.... Continue Reading >>

How to record audio using AVAudioRecorder

While it's not hard to record audio with an iPhone, it does take quite a bit of code so give yourself a few minutes to get this implemented. First you need to import the AVFoundation framework into your view controller, like this:... Continue Reading >>

How to record user videos using ReplayKit

ReplayKit is one of many useful social media features built into iOS, and it's trivial to add to your projects. What's more, it's not just for games – you can record any kind of app just fine. I should add, though, that the recording quality is fairly low, so it's not worth trying to record fine details.... Continue Reading >>

How to render a UIView to a UIImage

You can render any UIView into a UIImage in just four lines of code, and that even handles drawing all the subviews automatically. Here's the code:... Continue Reading >>

How to save a UIImage to a file using UIImagePNGRepresentation

If you've generated an image using Core Graphics, or perhaps rendered part of your layout, you might want to save that out as either a PNG or a JPEG. Both are easy thanks to two functions: UIImagePNGRepresentation() and UIImageJPEGRepresentation(), both of which convert a UIImage into a Data so you can write it out.... Continue Reading >>

How to scan a barcode

iOS supports barcode scanning out of the box, but to be honest it's not that easy to do. So, here's a complete UIViewController subclass that you can add to your Swift project and get immediate support with no hassle – all you need to do is update the found(code:) method to take some interesting action, then present this view controller when you're ready:... Continue Reading >>

How to scan a QR code

iOS has built-in support for scanning QR codes using AVFoundation, but the code isn't easy: you need to create a capture session, create a preview layer, handle delegate callbacks, and more. To make it easier for you, I've created a UIViewController subclass that does all the hard work for you – you just need to modify the found(code:) method to do something more interesting.... Continue Reading >>

How to turn on the camera flashlight to make a torch

There is one simple property required to enable or disable a device's torch, but you do need to put in some wrapper code to make it work safely. Specifically, you need to use the lockForConfiguration() and unlockForConfiguration() methods of the AVCaptureDevice class in order to make sure only one app can control the torch at a time.... Continue Reading >>

UIImageWriteToSavedPhotosAlbum(): how to write to the iOS photo album

It's not hard to save an image straight to the user's photo library, but I have to admit the syntax isn't immediately obvious! iOS has a function called UIImageWriteToSavedPhotosAlbum() that takes four parameters: parameter one is the image to save, parameters two and three set a delegate and selector to send when the image has been written successfully, and parameter four is any additional context information you wan to send.... Continue Reading >>

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 4.

Download for free!

Want a free 75-minute video teaching functional programming, protocol-oriented programming, and more? This is your lucky day!

MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>