<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andrey Yutkin on Medium]]></title>
        <description><![CDATA[Stories by Andrey Yutkin on Medium]]></description>
        <link>https://medium.com/@hivehicks?source=rss-e0cee70a831a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*SlWreXBPdSyZEAjN.jpg</url>
            <title>Stories by Andrey Yutkin on Medium</title>
            <link>https://medium.com/@hivehicks?source=rss-e0cee70a831a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 22:30:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hivehicks/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Paparazzo. Open Source media picker for iOS apps. Part II]]></title>
            <link>https://tech.olx.com/paparazzo-open-source-media-picker-for-ios-apps-part-ii-b2b58ee1d686?source=rss-e0cee70a831a------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2b58ee1d686</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Andrey Yutkin]]></dc:creator>
            <pubDate>Mon, 12 Jun 2017 11:32:07 GMT</pubDate>
            <atom:updated>2017-06-12T11:32:07.412Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*harg-WGXEoJXMG2ivsEOFg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fBroX83a7MblIcN1." /></figure><p>In <a href="https://tech.olx.com/paparazzo-open-source-media-picker-for-ios-apps-part-i-c22c95a2c9fc">Part I</a> we discussed the options we reviewed when implementing custom camera in Avito iOS app and shared some implementation details of the final solution. Now it’s time to continue the story.</p><h3>Photos from multiple sources</h3><p>Our photo picker can deal with photos coming from three different sources:</p><p>● Photos taken with the camera are kept in the app’s folder on disk.</p><p>● User can select photos from the their photo library.</p><p>● When editing an existing ad, photos can come from the web.</p><p>Rather than having three separate entities for each of these cases, we wanted to have a single one to avoid ugly code branching when manipulating photos and to protect the code from changes, should some new image source be added.</p><p>We have identified four typical actions performed on an image:</p><p>● The most common one is, obviously, displaying the image in UI. Ideally, we would prefer not to keep a full-size photo as large as 3000×4000 pixels in memory if we just need a small thumbnail.</p><p>● Next is receiving the original image — for example, in order to send it to a server or save it to disk. Again, in this case we want as low memory consumption as possible, so a compressed NSData representation of an image would be sufficient — we don’t want to spend system resources on converting, say, a JPEG picture into a bitmap.</p><p>● Sometimes it’s useful to know image size, and getting the size should be optimized to avoid (if possible) loading the image into memory for this purpose only. Often, the size can be obtained from the image metadata or from a server that can send it as a separate property in the JSON next to the image URL.</p><p>● Finally, if the image is being downloaded from the web, but at some point we understand that we no longer need it (for example, we have closed the screen where it should have appeared), it would be nice to be able to cancel the download.</p><p>Since the image may not be available locally at the time we need it, the API in the first three situations should be asynchronous. To display image in UI without loading redundant data into memory, we need to find out what size of it we need.</p><p>For this purpose, we need to know the size of the area it will be displayed in and how we want to use it: whether we want to fill it with the image entirely or it’s okay to sacrifice some of its parts so that there is no empty space left inside (similar to UIViewContentMode.aspectFit and .aspectFill).</p><p>Since the API must be asynchronous, we need a handler where we’ll be setting UIImageView.image property.</p><p>It may also happen that we need to download a photo from the web, and we already have a smaller sized version of the same image cached locally. It turns out that if we display this smaller version in the view while the original image is downloading, the user gets the impression that the download is faster. That’s why we need ‘deliveryMode’ parameter — by setting it to ‘progressive’ we indicate that we do not mind lower-quality versions of the requested picture, and the handler can be called multiple times as the quality improves. ‘best’ means that we want the handler to be called only once passing us the best version of the picture.</p><p>As a result, the image request method with the described parameters may look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc8f99b556bec823428a3f54d760fe45/href">https://medium.com/media/cc8f99b556bec823428a3f54d760fe45/href</a></iframe><p>We can simplify it by combining the first three parameters into a structure. It allows us to add other parameters in the future without changing the method signature.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/198a9c3ab5ea7fc745c519c3bfa036b5/href">https://medium.com/media/198a9c3ab5ea7fc745c519c3bfa036b5/href</a></iframe><p>The resulting code still needs improvement. First, the handler’s closure parameter clearly indicates the UIImage type, while we wanted to get rid of UIKit dependency to make this method compatible with platforms other than iOS. So UIImage has to be replaced with something that may be subsequently converted to UIImage. There is a type that meets this criterion and is present on both iOS and macOS — CGImage. This led us to declaring InitializableWithCGImage protocol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46335b5c97c44c0a163a597905392d8c/href">https://medium.com/media/46335b5c97c44c0a163a597905392d8c/href</a></iframe><p>Fortunately, UIImage and NSImage already have these initializers, so all we have to do is to add blank extensions, formally indicating their conformance to the protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9799ffeb3886fee66b7d2cd6bd9aa8da/href">https://medium.com/media/9799ffeb3886fee66b7d2cd6bd9aa8da/href</a></iframe><p>After replacing UIImage with this protocol, we end up with the following method signature:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a551f5ab8567ec6c45c1267cfc289acb/href">https://medium.com/media/a551f5ab8567ec6c45c1267cfc289acb/href</a></iframe><p>Finally, we want to ensure the possibility of canceling the request. To do this, we add return value of type ImageRequestId to ‘requestImage’ method, that will give us the opportunity to identify the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69a16e69a5f9cbcec79a788d5d46e9f2/href">https://medium.com/media/69a16e69a5f9cbcec79a788d5d46e9f2/href</a></iframe><p>There is only one minor modification left.</p><p>Earlier, I said that if deliveryMode is set to ‘progressive’, the handler may be called multiple times. Ideally, it should be possible to be able to figure out within the handler whether it has been called with the final or intermediate image version. Therefore, we will call it with ImageRequestResult structure as the parameter. In addition to the image itself, this structure will contain other useful information about the result of the request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95778c9d486773eb793b585ba0fc4767/href">https://medium.com/media/95778c9d486773eb793b585ba0fc4767/href</a></iframe><p>This is the final version of the method for displaying the image in UI.</p><p>Three other methods are simple, two of them are essentially asynchronous getters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/444e906c51ec11b95f97013cc63e2279/href">https://medium.com/media/444e906c51ec11b95f97013cc63e2279/href</a></iframe><p>Now we have ImageSource protocol, perfectly suited for use as a model of our photo picker, and the only thing left is to implement it for three possible cases: photos from the built-in storage, from the web, and from the user’s photo gallery.</p><h3>Photo gallery</h3><p>Starting with iOS 8, access to the photo library is provided by Photos.framework. The library itself is represented by PHPhotoLibrary instance, and photos in it are represented by PHAsset objects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*onO5MEZN_zlQ8Bww." /></figure><p>To get photo representation that can be displayed in UI, we use PHImageManager to convert PHAsset to UIImage.</p><p>The method behind this transformation has the following signature:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4eac508bef5bb4822087ee11d415b0b/href">https://medium.com/media/d4eac508bef5bb4822087ee11d415b0b/href</a></iframe><p>As you can see, it looks very similar to ‘requestImage’ method in our own ImageSource protocol. That’s because the first implementation of ImageSource was basically a PHAsset wrapper, so we largely relied on that signature.</p><p>Unfortunately, while working with PHImageManager, we encountered some problems.</p><p>The first problem became obvious when we were working on the task of displaying photos in collection view. PHImageManager doesn’t guarantee that resultHandler will be called when the request is canceled. Sometimes it will be called, but image will be nil. We wanted to simplify the client code so that it did not have to figure out what had happened. Thus, a strict set of rules for calling resultHandler was developed, one of which stated that resultHandler must not be called after canceling the request.</p><p>The solution to this problem was fairly simple. PHImageManager’s resultHandler is given two input parameters — the first one is UIImage, and the second one is ‘info’ dictionary containing useful information. This information includes a flag allowing to determine whether the request has been canceled. But this flag may not be there if the request was canceled after this resultHandler call was added to the queue. So we had to store an array of canceled requestIds and search for the request in it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc5777c749c503a16f3a6f644a8fab33/href">https://medium.com/media/dc5777c749c503a16f3a6f644a8fab33/href</a></iframe><p>The second problem came when we were working with a photo from iCloud, and we needed to show the activity indicator during the download. The only possibility to track the download is to set ‘progressHandler’ closure in PHImageRequestOptions object, which is then passed to PHImageManager when the image is requested.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9978c1a5789ecfbabb61942db57e079c/href">https://medium.com/media/9978c1a5789ecfbabb61942db57e079c/href</a></iframe><p>We needed to track only when download started and finished, so we added two closures to our own request parameters structure. And if in case of starting we simply activated onDownloadStart at the first call of progressHandler, onDownloadFinish required a bit more work.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f0795404c4179207cd42813d297e77f/href">https://medium.com/media/5f0795404c4179207cd42813d297e77f/href</a></iframe><p>If we were lucky and progressHandler reported that the picture download was 100% complete, which corresponds to the value ‘progress == 1’, we called onDownloadFinish at this point.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bb174369cb3951e7b5c08cffb07b145/href">https://medium.com/media/7bb174369cb3951e7b5c08cffb07b145/href</a></iframe><p>However, the trick is that it may not happen, and the last call of progressHandler is at a progress value of less than 100%. In this case, we try to guess whether the download is complete or not inside resultHandler. The ‘info’ dictionary, which we receive in this callback, contains IsDegraded flag indicating whether we have received the final or an intermediate version of the image. Here it is logical to assume that the download is complete either if we have canceled the request or if the final version of the picture has been received.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4109f7235ab8c6265e5d69436f6b82b9/href">https://medium.com/media/4109f7235ab8c6265e5d69436f6b82b9/href</a></iframe><p>You can explore the implementation of ImageSource for photos from disk and from the web in<a href="https://github.com/avito-tech/Paparazzo"> Paparazzo repository</a>.</p><p>Our media picker has already caught the attention of iOS developers, including<a href="https://maniacdev.com/2017/03/paparazzo-an-elegant-swift-based-ui-component-for-photo-picking-and-editing"> global websites</a>. They report that our photo picker has excellent performance, elegant and straightforward implementation. You are welcome to try it out, test, discuss. Avito team is always happy to answer your questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2b58ee1d686" width="1" height="1" alt=""><hr><p><a href="https://tech.olx.com/paparazzo-open-source-media-picker-for-ios-apps-part-ii-b2b58ee1d686">Paparazzo. Open Source media picker for iOS apps. Part II</a> was originally published in <a href="https://tech.olx.com/">OLX Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Paparazzo. Open Source media picker for iOS apps. Part I]]></title>
            <link>https://tech.olx.com/paparazzo-open-source-media-picker-for-ios-apps-part-i-c22c95a2c9fc?source=rss-e0cee70a831a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c22c95a2c9fc</guid>
            <category><![CDATA[mobile-app-developers]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[olx]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Andrey Yutkin]]></dc:creator>
            <pubDate>Mon, 22 May 2017 13:45:11 GMT</pubDate>
            <atom:updated>2017-05-24T09:29:25.213Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pA8LP3Su8vqM9xx-SVEiSg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fLzBPoN2im0ZEqke." /></figure><p>A while ago we were faced with the task of radically redesigning the process of publishing ads via Avito iOS app. The result had to be a tool that would make this process quick and convenient for the user. Obviously, the buyer prefers to see what he is going to pay for. Therefore, giving the seller the capability to easily add and edit photos was one of our top priorities. Here you can read how we achieved this.</p><p>Running a bit ahead of the story, our media picker is called Paparazzo and it’s available on <a href="https://github.com/avito-tech/Paparazzo">Github</a>. In this article, we’ll discuss the technical details of capturing camera image and displaying it in several UIViews at the same time.</p><h3>UIImagePickerController and open source</h3><p>The simplest way to implement camera in iOS app is to use UIImagePickerController.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*4H8rYNjVT-MPxjfc." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*gY_yZ1uOeimfia_R." /></figure><p>Its functionality is quite restricted, that imposes certain limitations:</p><p>● It can operate either in camera mode or in gallery mode, while we needed a hybrid mode.</p><p>● It has to be presented modally, because it’s actually a UINavigationController that’s not allowed to be embedded in another UINavigationController, while our camera should’ve been one of the steps in a linear sequence of screens.</p><p>● UIImagePickerController produces UIImage, which is an uncompressed version of an image stored in memory and takes up a lot of space. When working with UIImagePickerController in my previous projects, I’ve noticed that on slow devices like iPhone 4 (and sometimes even iPhone 5) an app sometimes crashed due to memory warning even before delegate method that returned image to a client code was called. We wanted to avoid inefficient use of RAM in order to prevent such problems.</p><p>We also explored some of the available open source solutions, but they fell short of our requirements. Some lacked the necessary features, such as image crop or rotate, others allowed selecting only one photo, without supporting a filmstrip showing selected photos. All in all, the user flow implemented in those components did not suit us. Therefore, we decided to implement our own camera from scratch, so that we could make quick improvements in the future.</p><h3>AVFoundation</h3><p>Another way to implement camera is to use the low-level framework called AVFoundation. It allows to get the most out of the photo, video, and audio recording and playback capabilities offered by iOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dvFOSIRiAnLfNMGs." /></figure><p>The central object in AVFoundation is AVCaptureSession, which coordinates the flow of data from a capture source to a client. In order to do something useful, though, it needs some inputs that actually provide data to the session.</p><p>Inputs are represented by instances of AVCaptureDeviceInput, and they originate from AVCaptureDevice, which is a physical device, such as the front camera, rear camera, or microphone. In addition to the media input, we also have to define the output.</p><p>For this purpose, there are one or more AVCaptureOutputs on the other side of AVCaptureSession. Examples of outputs include AVCaptureStillImageOutput (for photos) and AVCaptureMovieFileOutput (for videos). Each output can receive its content from one or more inputs (for example, AVCaptureMovieFileOutput can receive both video from a camera and audio from the microphone).</p><p>Inputs and outputs are connected by one or more AVCaptureConnection instances, so if, for example, we need to record video without audio, we wouldn’t establish a connection between AVCaptureMovieFileOutput and the microphone input.</p><p>Configuring AVCaptureSession is quite easy. First, we need to get a list of devices that support video capture. After that, we find the rear camera, checking the value of the ‘position’ parameter of each device. Finally, we initialize AVCaptureDeviceInput passing it the camera object as a parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c1c890db9cfeea5573fb47b40cbe39e/href">https://medium.com/media/6c1c890db9cfeea5573fb47b40cbe39e/href</a></iframe><p>Setting up an output is even easier: we simply create AVCaptureStillImageOutput and set the codec to be used during the capture.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be820bb72b170b21ef38f9d415096f45/href">https://medium.com/media/be820bb72b170b21ef38f9d415096f45/href</a></iframe><p>Now that we have both the input and output, we can proceed to creating AVCaptureSession</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/837d953d0fa7144c5a8d44b73b5e1303/href">https://medium.com/media/837d953d0fa7144c5a8d44b73b5e1303/href</a></iframe><p>‘sessionPreset’ property allows you to set quality, bitrate and other output parameters. Apple provides 14 default presets which are suitable for the majority of common tasks. However if you’re not satisfied with the result, you can set specific properties on the AVCaptureDevice instance representing the physical capture device itself.</p><p>Before adding inputs and outputs to the session, you must check whether it’s possible to do so by calling canAddInput and canAddOutput methods, otherwise the app may crash.</p><p>Then we call startRunning() on the session to start transferring data from inputs to outputs. Note that startRunning() is a blocking call, and its execution may take some time, so it’s recommended that you configure the session in the background to be sure that the main thread is not blocked.</p><p>AVFoundation also includes AVCaptureVideoPreviewLayer class. It’s a CALayer subclass that’s initialized with an instance of AVCaptureSession and displays the preview from the camera. We need to simply add this layer to the right place, and everything works automagically. What could possibly go wrong? However, in our case it turned out not to be as straightforward as it seemed at first glance.</p><h3>Displaying camera preview</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fyc8aBp67rHo%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dyc8aBp67rHo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fyc8aBp67rHo%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7b79716e1ce66f29c51163acc5b23fa/href">https://medium.com/media/c7b79716e1ce66f29c51163acc5b23fa/href</a></iframe><p>As you have probably noticed, in the lower right corner of the screen, in addition to the icons of the selected photos, there is one more icon with a live preview of the camera, which duplicates the main preview. It was not included in the original design. When it appeared as a result of further development of the UI, we thought that it was just a matter of adding one more AVCaptureVideoPreviewLayer and linking it to the existing AVCaptureSession. But we were wrong. A single AVCaptureSession can send output to a single AVCaptureVideoPreviewLayer only.</p><p>Then we tried creating two sessions, with each of them sending its output to its own AVCaptureVideoPreviewLayer. But that did not work either. As soon as the second session starts, the first one is immediately terminated.</p><p>Further research led us to a solution. In addition to CaptureStillImageOutput, that out initial setup included, we had to add a new output of class AVCaptureVideoDataOutput.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vkLrca5UoDkGv4c9." /></figure><p>This output has a delegate, which, in turn, has a method that provides us with each camera frame, making it possible to do anything with it, including manual rendering.</p><p>The output provides frames in the form of CMSampleBuffer objects. Data represented by this object can be efficiently rendered by a GPU using OpenGL as well as Apple’s own low-level framework Metal, that was introduced recently. According to Apple,<a href="https://clearbridgemobile.com/will-apples-metal-framework-fully-replace-opengl-for-ios-development/"> Metal can be up to 10x</a> faster than OpenGL ES, but it’s supported only on iPhone 5s and newer devices. So we settled on OpenGL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6h-HQ1ES0UT9rULk_TKsAw.png" /></figure><p>In order to manually render camera frames you must implement captureOutput(_:didOutputSampleBuffer:from:) method of AVCaptureVideoDataOutputSampleBufferDelegate protocol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca329225b11eb2feaf6cdc49a98dbd53/href">https://medium.com/media/ca329225b11eb2feaf6cdc49a98dbd53/href</a></iframe><p>A bit later you will see that almost all the rendering is performed by Core Image. However Core Image cannot operate directly on CMSampleBuffer object, it needs CVImageBuffer, so we transform one object into another.</p><p>Note that at this stage we check ‘isInBackground’ flag. No OpenGL calls can be made when the app is in the background, otherwise the system will immediately terminate it. I’ll show how to avoid this later, but for now just remember this flag.</p><p>Then we iterate the views which will be displaying the preview, and pass them the resulting imageBuffer. All these views are instances of our class GLKViewSubclass.</p><p>This class, as you have probably guessed by name, is a GLKView subclass. Like any other GLKView, this view is initialized with an OpenGL ES context. What sets it apart from the others is a Core Image context, which will perform the rendering of image buffer’s content.</p><p>The implementation of the draw(_ :) method is shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f40754771dfaa2b872251e7b7e589e9/href">https://medium.com/media/2f40754771dfaa2b872251e7b7e589e9/href</a></iframe><p>‘drawableBounds’ method simply converts CGRect specified in points to a pixel CGRect, because Core Image deals with pixels and does not know whether the display is Retina or not.</p><p>‘sourceRect’ method returns a rectangle corresponding to a fragment of the frame being displayed that fits into the view, given its aspect ratio. In other words, if the frame has an aspect ratio of 3:4, but the view is a square, this method will return a central part of the frame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OtTiPVLr1DbIID1W." /></figure><p>As I said, OpenGL calls cannot be made when the app is in the background. To guarantee this, we need to handle UIApplicationWillResignActive and UIApplicationDidBecomeActive events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e0cc43298c0bd1aa70f800b1c177c7a/href">https://medium.com/media/8e0cc43298c0bd1aa70f800b1c177c7a/href</a></iframe><p>Both notifications are sent on the main thread, but messages from the AVCaptureSession delegate are delivered on a background thread. To ensure that no rendering occurs after the first handler has been executed, you need to synchronously switch to the delegate’s queue, call glFinish(), and set ‘isInBackground’ flag, that the output delegate will check before frame rendering.</p><p>The second event should also be handled on the delegate’s thread, but this can be done asynchronously, because in this case the delay is not critical and will not have any negative consequences.</p><p>To sum it all up, the general implementation is as follows: CaptureSession sends the camera frame to VideoDataOutput, which forwards it to its delegate, and the delegate, in turn, sends the data to the respective views, unless the app is in the background, and finally, the views render the frame themselves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6g-oV2QCYJP7B7yj." /></figure><p>Now the problem of displaying the camera preview is solved.</p><p>In this article, we have discussed using AVFoundation to capture the image from the camera and displaying it in several UIViews simultaneously. In the forthcoming article we will focus on the abstraction, which we introduced to handle photos from different sources (such as disk, network or user’s photo library) identically.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c22c95a2c9fc" width="1" height="1" alt=""><hr><p><a href="https://tech.olx.com/paparazzo-open-source-media-picker-for-ios-apps-part-i-c22c95a2c9fc">Paparazzo. Open Source media picker for iOS apps. Part I</a> was originally published in <a href="https://tech.olx.com/">OLX Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>