MotionJpegImageViewMotion-JPEG streams are really nothing more than constantly updating JPEG image files. As such, it seemed appropriate to base the Motion-JPEG view on Apple's existing UIImageView class. Rather than providing a UIImage file, however, we fill MotionJpegImageView by providing it a URL for the video stream. Internally, MotionJpegImageView creates a connection to the stream and receives frames one at a time over the network. As it receives each frame, it hydrates the stream data into a UIImage object, and then sets its inherited image property. Thus, the only limitations are
- The speed of your network connection
- The frame rate of the IP camera
- The phone's ability to process inbound packets and rehydrate UIImage files from raw JPEG data.
Secure Cameras and StreamsBasic video streaming is pretty simple. However, most people will want to secure their video feeds with a username and password, and often encrypt the stream using HTTPS. MotionJpegImageView can handle these feeds, as well. When it comes to handling secure streams, there are a couple of options. First, you can load an encrypted stream simply by passing a URL with the HTTPS scheme at front. In order to make HTTPS work, the server (your IP camera) needs to have a certificate installed. While businesses may have the resources to pay for a publicly trusted certificate from a regular certificate authority, individuals will often resort to installing a "self-signed" or "standalone" certificate. The advantage of a self-signed certificate is that you can save yourself a couple of hundred dollars; the disadvantage is that you may be vulnerable to a "man in the middle" attack, in which a cracker intercepts requests you send to the IP camera and can therefore gain access to things like the username and password you are using. For use on a secure local area network in a private residence, the chance of a man-in-the-middle attack is quite low, and therefore the cost of the certificate outweighs the benefit. As a result, if you want to access an HTTPS video feed on a camera with a self-signed certificate, all you need to do is set the "allowSelfSignedCertificates" property to YES (it is NO by default):
_imageView.allowSelfSignedCertificates = YES;Another common security option is the ability to password-protect the video feed. Password protection is all well and good, but if you use it without also using an encrypted data stream, your username and password are sent over the wire using "clear text" -- meaning that anyone who knows how to install one of the free network sniffing utilities such as Wireshark can easily discover your login credentials. Although we recommend that you use both a password and an encrypted stream, if you want to allow clear text logins, you can set the "allowClearTextCredentials" property to YES (again, this is NO by default):
_imageView.allowClearTextCredentials = YES;Finally, you can set the username and password on your stream programmatically using the username and password properties, or you can leave these blank. If you do attempt to access a password-protected stream and the login credentials are either blank or incorrect, MotionJpegImageView will display a dialog box in which the user can enter the appropriate information. Credentials persist for the duration of the session -- i.e., once you close the app, you will need to re-enter credentials when you attempt to access the stream again later.
- Much improved frame rate (when compared with UIWebView). 7x to 8x speed improvement when loading the same size stream under the same network conditions. More efficient processing makes it possible to load more streams simultaneously; whereas UIWebView tops out with one or two feeds, MotionJpegImageView can work reasonably well with up to 8 streams.
- Easy handling of passwords and secure data streams.
- Simple resizing. With UIWebView, resizing can be tricky. Using the "scalesPageToFit" property will fit the feed within the frame of the UIWebView without scrolling, but often the page will render with extra whitespace around the edges. If you know the exact dimensions of the feed, you can manually scale the content using an affine transformation, but it can be difficult to determine image dimensions reliably at runtime. By contrast, MotionJpegImageView automatically sizes content to fit according to the option specified in its "contentMode" property (which is set to "UIViewContentModeScaleAspectFit" by default).
- Convenient play/pause/stop/clear options. With UIWebView, you can either load the page or stop loading the page, and reloading can take a couple of seconds. With MotionJpegImageView, you can play or pause the stream quickly and easily. You can also stop (which clears the screen), or blank out a paused feed using the "clear" method.