Is it possible for a user to draw a dotted line (in a circle) around the bit of the UIImageView they wish to crop to, and then for the UIImageView to resize to those points? It's a bit like the lasso/marquee effect in Photoshop:
Update: Since iOS 8.x, UIImageView provides a maskView property. Just prepare an image with some opaque pixels, create another image view with that mask image, and set this mask image view as the maskView. The opaque pixels in the mask image will be the ones shown in the underlying image.
UIImageView *maskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"somemask"]];
maskView.frame = imageView.bounds;
imageView.maskView = maskView;
// imageView will be masked by maskView
.
Original Answer
There's quite a bit to do, but here's a high-level outline:
create an empty image which will become a mask
build a path in that image from user input
fill the path to turn it into a mask
apply the mask to the imageview's image
Create image: A simple idea here is to just ship a black image with your project. It should be sized to match the maximum region a user can select. Read the image into memory (UIImage imageNamed:) and set it up as the drawing context by calling UIGraphicsBeginImageContext.
Create path: (see apple docs). When the user starts stroking the region, call CGContextBeginPath, then follow user gestures, sampling the touches and adding small segments by calling CGContextMoveToPoint repeatedly as touchesMoved.
Create mask: To turn the path into a mask you want a black background and the path filled with white. If you started with a black image, you just need to do the fill. See the same apple guide about doing that.
Finally, you'll apply this mask to your imageView's image. Here's a decent reference for that.
This answers just one part of your question i suppose, but here's an example on how to draw and animate the dotted line (i.e. marching ants effect).
Related
I have to make view where there will be multiple curved or irregular shaped imageview. Now when i will touch a UIImageview then it will import image from photo gallery.For example: Suppose
I have an UIView with background image.
On that view i have 3 irregular shaped UIImageview.every UIImageview is attached with each other.
Now suppose i have touch in the first UIImageview then it will take an image from photo gallery.
When i have imported the image then that image will be as the background image of the UIImageview.
I have to make sure that if my UIImageviewis smaller then its background image then i will able to move the image or rotate the image or scale or zoom the image to adjust the image according to the size of UIImageview.
How can i do that whole process??? any help by link,source code,tutorial or anything else will be cordially appreciable.
Basically my first priority is that how can i make those irregular shaped UIImageView pro-grammatically.
IF ANY FURTHER QUERY TO ASK THEN PLEASE DON'T HESITATE.
Thanks In Advance
The one way of doing this is as follows:
Subclass from UIImageView set touchesBegan methods for each UIImageView for import image, setting on background and whatever you want to do on touch.
What does 'attached' mean? If all of them should move together, then, again, you have to deal with it in touchesBegan methods, sending messages like 'Hey views, i've moved on this vector:[vector], please do it too'.
In pt. 3.2 i didn't get your point. :( You can set in UIImageView subclass self.contentMode = UIViewContentModeScaleAspectFit or UIViewContentModeScaleToFill, so its image will always fit the view.
About irregular shape, did you read links below? I suppose there's not an super easy way to do it :(
http://www.iphonedevsdk.com/forum/iphone-sdk-development/76923-hittest-collision-for-irregular-shaped-uiviews.html
Draggable UIImageView Partially Transparent & Irregular Shaped
Simple way of using irregular shaped buttons
I want to add an image onto another image. But I know that imageview has a rectangular frame. Is there a way I can add an image without rect farme? Say, I want to add a pin onto an imageview. will it necessarily be a rectangular shaped imageview or can I add an image with frame shape that of a pin.
It is rectangular you can not add imgeView with other shape.
What you need is an transparent png that is your pin and added it to your original image view like an sub view.
i want to crop image via drawing circle using touchMoved event.
so how can i find the circle point in CGRect .
i have to just pass that cgrect to cropping function.
Pls Help me for drawing the circle on image and find the CGRect
Thanks
I'm not sure that I completely understand your question. I will make some assumptions:
I assume you want to display a circular region of an image, with the image cropped to only display its content within that circle; and
I assume you will be moving this circle around as the user moves his/her finger.
I will assume the non-cropped area is a solid color.
For this case, I would recommend creating a transparent png image file with a circle of transparency, and the solid background color everywhere else. You can put this in a drag-and-drop-able UIView subclass, and when the user moves his/her finger around, it will appear as if the image is being cropped in a circle that moves with their finger.
Here's how to make a UIView subclass work with drag-and-drop:
http://bynomial.com/blog/?p=77
If you need more sophisticated behavior, such as a non-solid-color background, then you could consider using an image mask with your view's CALayer property. This would be more work, because you'd have to modify the mask every time the user moved their finger. Another option would be to set the CALayer to have rounded corners (the cornerRadius property), set clipsToBounds=YES, and then move that around appropriately when the user moves their finger.
Reference for some CALayer properties, to get a sense of the cornerRadius approach (set the cornerRadius = half the width of the view, and it will be a circle):
http://bynomial.com/blog/?p=52
I have a UIView has some labels and buttons on it.
Next I also have a image which has a square area that is transparent, now
if I create a UIImageView and add this image which has transparent regions I am not able to see the background view (which has buttons and labels) through this transparent image.
If I play with the alpha value that doesn't work as intended which is to see the transparent regions exactly as it would have appeared on the UIView which has the labels and buttons.
UIImage* image = [UIImage imageNamed:#"TI1.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Also I would be interested to know if there is other way to achieve what I am trying to achieve.
Basically I want to highlight a certain area of the view which has buttons/labels and make the rest of the area greyed out. My idea was to have this UIImageView with transparent regions in image to achieve that.
Thanks
Ankur
Try setting imageView.opaque = NO;
UIImageView inherits from UIView. According to that class's docs:
This property provides a hint to the drawing system as to how it
should treat the view. If set to YES, the drawing system treats the
view as fully opaque, which allows the drawing system to optimize some
drawing operations and improve performance. If set to NO, the drawing
system composites the view normally with other content. The default
value of this property is YES.
Also, not sure that JPG even supports transparency, so try exporting the image as a PNG to ensure you get the results you're looking for.
I have a custom view for which I've (in the XIB) created a UIImageView as a subview. How can I make the image view appear in the background? (i.e. so that in the custom view I can create the hands of a clock that will appear over the top)
Do I need to make my background image have alpha areas for transparency? (if I have the terms correct) Or is it OK to have an image with it's background just set to white or black or whatever it needs to be (on the basis it will be in the background so this will be ok)
You can't: views are composited on top of their superviews, which means if your custom view is drawing in -drawRect:, it will always appear under the UIImageView.
The easiest way to achieve the effect you're looking for is to put the image view under your custom view in IB.
Background images generally do not need to have transparency (and there's a slight speedup if the PNG has no alpha channel).
An alternate approach to using UIImageView objects if you are drawing the rest of stuff in drawRect: is to use the image directly. Draw the image in drawRect: before you do any of the drawing.
Example
- (void)drawRect:(CGRect)rect {
[backgroundImage drawInRect:rect];
/* Do rest of your drawing */
}
Now this is only if you are using a complex image. If it is a texture, you would rather use UIColor's colorWithPatternImage: method to get a color from the UIImage object and set your custom view's backgroundColor. Remember that using the colorWithPatternImage: will not scale the image while drawing and will tile if the source image is smaller the frame of the view.