How to create a non-rectangular UIImageView - ios

I'm trying to make a ViewController that presents info from a webpage like this:
However, I'm confused on one thing. How did they get the imageView to display an image that's cut off at the corner, i.e. not rectangular? Do you think they created that player card in Photoshop and used it as the background for the imageView image, or did they create it programmatically?
I wonder because the image is behind the picture of the bear, so I imagine if they created the background in Photoshop, how would they get the image behind the bear head? They can't have just created the card with the player's picture as part of it, then loaded the whole image because if they traded the player, because I'm sure they pull the player info and picture from the web so they can have a card for all players, even if they trade or acquire a new player mid-season, without having to update the app (and add the finished image to images.xcassets).

This can be composed from two CALayers at runtime. Put the picture on the bottom layer; the picture can come from anywhere - the web, the bundle, etc. the image source could be dynamic.
Put another CALayer on top, with the frame rendered with opaque colors, and a transparent cut-out for the picture in the middle:

There are a bunch of ways to do this. A simple and flexible way to do it is to create a CAShapeLayer that's the same size as the image view, with it's origin at 0,0, and add it as the UIImageView's layer's mask.
You'd create a filled UIBezierPath that maps out the part of the image you want to show, and install the bezier path's CGPath into the mask layer's path property.
The result would be that the image view is cropped so that only the part inside the shape is drawn.

Related

UIView position relative to photo taken

I have a UIViewController which consists of several UIView elements. From this view controller a photo is taken using AVCapturePhotoOutput.
Then, from the output, UIImage is built.
Is it possible to know the position of a specific UIView seen on a preview layer (lets say SpecificView) in the result image? For example, I'd like to draw a horizontal line on the image at the position where SpecificView was when taking a photo.
I can get SpecificView.y relative to UIWindow, then multiply it by UIScreen.main.scale to get pixels but it doesn't seem to be solution.
Any ideas?

Show part of image in a circular arc

I have an image which is like a progress bar in circle. I need to show only a part of the progress bar based on the progress percentage. What I am trying to do is to put the image inside an imageview and show only a part of the image (arc section of the entire circle) based on the progress.
I have tried using UIBezierpath to create an arc shaped view and setting my progress bar image as the background image. But the image will try to fit inside the arc shaped view and looks bad.
Is there a better way to achieve the same?
Edit: Here is a sample image of what I am trying to achieve
But instead of the blue background, I need an image. So, only part of the image will be visible depending on the progress. (When its 100%, the entire image is visible)
I have spend almost half a day on this one. Any help would be appreciated.

Smootly mooving a hole in UIImage?

I have a UIImageView displaing an image. This view's layer is masked with CAShapeLayer in order to create circular "hole" in the image. To create the hole I use UIBezierPath with .usesEvenOddFillRule = true.
It works fine when static. But I need that hole to move with user finger. To do that I create new UIBezierPath with even-odd rule each time user moves their finger. On smaller phones with smaller images it looks OK but on iPhone 6 Plus it is choppy.
Any ideas on how to make it smooth are very wellcome. I cannot just move the frame of masking CAShapeLayer - it would move the hole bot also hide some edges of the image. So the only way is to change its .path each time user moves finger and that is slow.
EDIT: matt's answer would work in some scenarios but not in my case: I am not displaying the whole image only a part of it defined by UIBezierPath. This part is most often oval (but can be rectangular or rounded rectangle) and it has "hole" cut in it. While the hole is mowing with users finger the displayed part/shape of the image does not change - it is static.
The ineficient solution that was in place so far wa:
Create UIBezierPath with boundary of displayed part of the image
Set 'even off fill rule' on it
Add UIBezierPath of the hole to it
Set it as path of CAShapeLayer with some opaque fill color
Use that CAShapeLayer as mask of the UIImageView
This procedure was repeated each time a user moved their finger. I cannot simply move the whole mask layer as that would also change the part of the image being displayed. I what it to stay static and move only the hole in it.
it would move the hole bot also hide some edges of the image
Well, I don't agree. Moving the mask is exactly the way to do this. I don't see why you think there's a problem with that. Perhaps the issue is merely that you have not made the mask layer big enough. It does not have to be the same size as the layer it is masking. In this case, it needs to be about 9 times the size of the masked layer (3 horizontal and 3 vertical), so that it will continue to cover the masked the layer no matter how far in any direction the user slides it.

How to make a portion of an UIImageView transparent in order to show views in layers below

Here is a photo explanation to better illustrate what I mean:
I have two UIImageViews, with View1 being on the bottom and View2 at the top:
What I would like to do accomplish is to programmatically set an area on View2 that is completely transparent (i.e. has an alpha of 0), so that this will be the end result:
I haven't been able to find a similar problem related to marking a part of a UIImageView transparent in the form of a shape (specifically, a circle), and was wondering how I should tackle this problem?
Thanks!
One road you could go is CoreGraphics:
Create an image context
Set the clipping path you need (or just clear the circle for the "hole" after drawing)
Draw the original image into the context
Make an UIImage from that context
Assign the image to the top UIImageView
https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/HandlingImages/Images.html#//apple_ref/doc/uid/TP40010156-CH13-SW1

Apply a CIFilter to a UIView & all subviews

I have a UIView, that contains a UIImageView for its background. The view contains a number of segments (UIImageViews), which the user can drag and drop images to. I want to add a CIFilter to the whole view, such that any images that are subsequently dropped into the view will have the filter applied instantly. But the filter shouldn't be applied to the main view's background image. I realise that a CIFilter requires an input image, I was thinking of using a transparent input image, then placing the filter on top of all the views so it would only be visible when there is an image behind it.
Is it possible to supply a transparent input image to a CIFilter?
Is this the best way of going about it?
I suppose I'd need to mask the filter to the view containing the segments so that it is not applied to the background image. How would I go about this? Since a mask needs an image to mask to, I guess I'd need to render the segments view to an image and mask to this?
Thanks in advance

Resources