Pinch and Pan to Crop UIImage - iphone

I want to add a Pinch and Pan gesture that will crop the UIImage views on my ViewController. To crop the images, just pinch/pan the appropriate image and it will resize.
I currently have the Take Pic and Choose Image buttons grab the appropriate images into the UIImageView boxes accordingly, but I want to be able to pinch and pan to size and crop the image in the view.
I want the box to stay fixed at its current size, and the image within will be the one resizing and cropping.
PS. I am using storyboard
Here is a screenshot of what I have.
http://i.stack.imgur.com/chkk5.png
I was thinking that the right direction would be to add a swipe and pan gesture to each uiimageviews in storyboard, but not sure what to do after.

My general strategy for this in the past has to been place the UIImageView inside another UIView (in order to clip it) and add a UIPinchGestureRecognizer and a UIPanGestureRecognizer to the parent UIView. When you receive events for either of these you apply the appropriate transformation to the transform property of the UIImageView.
When scaling, for example, you would do the following:
-(void)scale:(UIPinchGestureRecognizer*)pinch
{
float scale = pinch.scale;
imageView.transform = CGAffineTransformScale(imageView.transform, scale, scale);
pinch.scale = 1;
}
The final step of reseting the scale is important because you are simply scaling the existing transform each time rather than a base identity transform. You should find that you will be able to handle translating the transform by using a UIPanGestureRecognizer and its translationInView: method (remember you will also have to reset this with setTranslationInView:CGPointZero.
Finally, in order to get a cropped/clipped UIImage out you can either capture the view's contents (I believe the transform property is only respected on iOS 7+) or alternatively render the UIImage into a CGContextRef, and using the same transform to transform the CGContextRef.

You can use below link...
Crop image

Related

Swift: hot to pan and zomm in/out image inside imageView

In ViewController I have a big ImageView whose image is captured with a ImagePicker. I want the user to be allowed to zoom in/out the image and then pan it but always inside the ImageView dimensions which must not enlarge, move or dwindle. I have seen I could add a ScrollView to ImageView but I can't figure out how to do that.
Moreover: is it possible to zoom in/out more than one ImageView at a time? I mean I have some smaller imageViews floating upon a bigger imageView. Is it possible to zoom everything all together?
There are plenty of excellent scrollview demo's around.
These will show you where and how to add override functions to implement what you need...
If you have different UIImage Views you can set the zoom programmatically to match the image being "zoomed" ... try experimenting with [UIImage].zoomScale. and the image offset. Its going to get complicated if they are different sizes as you will have to take account of scale when mirroring offset and zoom changes.

How do I replace an image in a rotated and scaled UIImageView?

I have a UIView with a UIImageView as a subview. I modify the UIView with CGAffineTransformRotate and CGAffineTransformScale. The UIImageView subview also transforms with it automatically.
I want to replace the UIImage in the UIImageView subview with a new UIImage. When I do this, the image is the wrong size and rotation, because the original been scaled and rotated.
How do I replace the image subview and have it appear as the previous image did (same size/rotation/location)?
Take another UIImageView2 and put your previous image in it first. Then after you scale and modify the original image to a new one and want to see your original one, you can show the UIImageView2 image.
I found the bug, I was removing the original image and didn't know it.
imageView.image = nextImage;
Changing the image as above keeps the scale and rotation from the parent. No trickery involved.

How to animate from UiImageView to a UIView

I use Path2.0. I have found that when I click the small picture in a message,there would be an animation to show the whole picture. How does it do that?
I would really appreciate help here. Thanks!
UiImageView is bascially a UIView, which you can apply animation on.
The use case you want to achieve combines:
a. a response to touch event, which you can use UITapGestureRecoginizer to attache a block of code
b. zoom the UIImageView into a proper size and position, you can use UIView's class method AnimiationWithDuration....
c. Ultimately, you want to change your UIImageView's frame property to a larger one. use CGRectMake to generate a new one. If you need to take the whole screen estate (like Path 2.0 does), you may need [UIApplication sharedApplication].window for this
I doubt they modify the original UIImageView; but a solution might be:
Copy the UIImageView (or UIImage)
Add it as a subview with the same exact frame as the original image
Apply a scale transform on the UIImageView (by using the CGAffineTransformMakeScale() function on a UIView's transform property (inside a UIView animation block)
Then instantiate the root window with a scaled version of the full image
Alternatively, you could instantiate the new subview with the full image, set it with the original UIImageView's frame, and then (using a UIView animation block), set it to the newer frame. To match their implementation exactly, I suggest a black subview (of the full UIImageView) with an alpha of 0.0 that you then animate at the same duration to an alpha of 1.0.

What is an UIImageView and how do I rotate it

From what I understand an UIImageView can contain a UIImage. Using the UIImageView you can then rotate the UIImage.
This, I believe, is done by assigning the UIImage an image of some sort and then, somehow, assigning the UIImage to the UIImageView.
I believe that I have "set" an image directly to a UIImageView and now wish to spin this image pinwheel style.
Is this possible or do I need to re-write so that my image is associated with an UIImage which is then, in turn, associated with the UIImageView?
I currently am using CGAffineTransformMakeRotation to attempt this on the UIImageView and the rotation looks more like it is flipping end over end and round and round.
Thanks
First, a UIImageView is just a view that knows how do draw an image on screen. That is it.
If you want to animate the spinning of an image you can either:
create separate images and load the array of images into the UIImageView; or
load a single image and use Core Animation to rotate the image.
Applying just a transform on the view will change it but won't animate it. That is what Core Animation is for.

UIImageView, UIView, taking a segment of a picture

I want to take an image from the UIImagePickerController and put it on a UIImageView that can move based on touch. I'm not sure how to get the picture onto the UIImageView. I thought the UIImageView automatically resizes the picture, so I couldn't grab just the top half of the picture and put that on the UIImageView. Is there an easy way to do this? thanks!
Use CGImageCreateWithImageInRect to crop (take a segment of?) a picture. Put a UIImageView inside a UIScrollView to let the user scroll through (move based on touch) a large image.
if you want to move and zoom your picture in a view, you need to use a UIScrollView. See Apple's ScrollViewSuite example. If you simply want to position the picture within a UIImageView, set the contentMode of the UIImageView to the setting you want e.g. scale, top, left, etc. See the UIImageView documentation for all options.