How to restrict the ImageView mask layer movement in iOS? - ios

I have applied the clipping mask effect (using the MaskImage + OriginalImage = Clipping mask effect image).
I set the UIPanGestureRecognizer to the UIImageView
The problem is I want to move the image alone not the mask image.

Maybe you could try to use hitTest to pass touches, but also wanna to know the details

Related

UIImageView corner radius results in gray unwanted sharp angle

I wonder why setting the corner radius of a ImageView will results in gray unwanted sharp angle, like this
Noted there is sharp gray angle behind the rounded corner image.
I set up the corner radius like this:
self.previewImageView.image = videoStream.thumbImage;
self.previewImageView.layer.cornerRadius = 8.0;
self.previewImageView.clipsToBounds = YES;
self.previewImageView.layer.masksToBounds = YES;
I also make sure the background color of the image view is white, but it wouldn't help anyway.
Anyone has any idea how to get rid of the sharp gray angle while setting the rounded corner of the image view?
I know I might draw a path and set the layer's mask path, are there any alternative?
My background view is a collection view cell, which happens to be not the same size as the imageView, i can't just set the corner radius of my background view
Try setting the image view's background color to clear.
just do:-
self.yourBackgroundView.layer.cornerRadius=self.yourImageView.layer.cornerRadius
You can try below code --
self.previewImageView.layer.borderColor = (__bridge CGColorRef)([UIColor clearColor]);
Hope this one is helpful.

How to remove gray color effect when touch move using objective c

I converted an image to fully gray when loaded, but I want to remove the gray color from it when touch move and view original image color.
I want to know how to convert from gray color effect to the original image and from original to gray when user moves finger over the image.
My solution is the following.
You need TWO UIImageViews. Add one on the other and make them to overlap pixel by pixel. You have two options:
B&W in foreground
Color in foreground
Both solutions fundamentally are the same. You need to use a mask on the topmost view. With a mask layer the white areas will be fully opaque, blacks are 100% transparent, grays are semi-transparent.
Example for the mask:
CALayer *maskLayer = [CALayer layer];
UIImage *mask = [UIImage imageNamed:#"mask.png"];
maskLayer.contents = (id)mask.CGImage;
maskLayer.bounds = (CGRect){CGPointZero, mask.size};
UIImageView *viewToMask = ;
viewToMask.image = [UIImage imageNamed:#"blackandwhite.png"];
viewToMask.layer.mask = maskLayer;
[self.view addSubview:viewToMask];
Certainly you have to add/remove the mask on touch/release. I leave you with that.
Now what happens when you move your finger? Just move the mask to different positions like so:
viewToMask.layer.mask.position = offset position based on touch position
I hope I could help you.

How do I aspect fit the zoomed part of an Image using UIScrollView?

I'm using self.myImageView.contentMode = UIViewContentModeScaleAspectFit; to aspect fit my image inside UIScrollView and inside gesture handling function I have this,
[self.myScrollView zoomToRect:CGRectMake(xZoomRect, yZoomRect, widthZoomRect, heightZoomRect) animated:YES];
but when I zoom into a part with zoomToRect method it doesn't move beyond the bounds of the contentSize.
For example, in the following image, orange rectangle is my image and I want to zoom into the blue part of the image. So I passed it's origin and size values to zoomToRect
but when I zoom in using zoomToRect it gets zoomed in in the following manner,
What I actually want is this,
How do I aspect fit the zoomed part of the image even when the part of the image to zoom is on the edge of the image, like I have described in the above images?
Instead of
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
use
self.myImageView.contentMode = UIViewContentModeScaleAspectFill;

iOS opaque UIView with a transparent section

I'm trying to make a simple view that is opaque except for a circle of a given diameter in the center. It is meant to overlay the camera, as they don't want the entire screen showing, simply that you center your face in the circle and snap the picture.
It's a few simple lines of code to to the opposite - a clear view with an opaque circle in the center - but I cannot figure out how to do the opposite.
Any help or pointers appreciated....
It sounds like you just want a simple mask. You can do that using Ash's method.
UIImage *_maskingImage = [UIImage imageNamed:#"mask"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = theView.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];
[theView.layer setMask:_maskingLayer];
You'll need an image that is a circle that has a transparency gradient going from black on the edge to transparent on the center. Remember which file type you're using as not all support transparency (like jpeg).
Alternatively, you could just use a UIImageView and have an image that has the gradient in it instead.
What about using the mask property of the view layer ?
Try something like this (the 2 views should have the same size) :
#import <QuartzCore/QuartzCore.h>
UIView *blackCircleOnClearBackground;
UIView *cameraView;
cameraView.layer.mask = blackCircleOnClearBackground.layer;

UIImageView gets blurry after layer transformation

I have a UIImageView add to a UIView as a subview. When I apply a transformation on the UIView's layer the UIImageView gets blurry. Why is that? How can this problem be resolved?
view.layer.position = newPosition;
I apply only this transformation.
Edit:
I've tested it and if I apply other transformations like these:
view.layer.transform = newTransform;
view.layer.zPosition = newZPosition;
then the blurry doesn't appear, only if I change the layer position.
Use
blurryDialog.frame = CGRectIntegral(blurryDialog.frame);
To set the frame coordinates to integer values
I found the answer. When you set the position of a layer or a view then the origin of it can be float values and this causes the blurry thing.
So the solution is to set also the frame's origin after you set the position. You just have to set the frame's position to integer values. On the UI this change won't be seen, but the image will not be blurry.

Resources