Control image size with slider - ios

I want to use a UISlider to control the size of an image. As you slide the slider one way the UIImage above should grow and as you slide the other way it should shrink. The ratio should stay the same. The math should be something like slider value = width, and height = (height/width ratio) * slider value. The way I think this could be done would be by making a variable and setting that variable to the image size and then making that variable = the slider value but I am not experienced with this sort of programming and do not know where to start. I found this question but I am not sure how to implement its code. Any point in the right direction, even just a conceptual explanation so I know how to write the code, is appreciated. NOTE- I did find this answer on changing the size, so now I just need to figure out how to link that to a slider.
EDIT- Now that the code is done and working, if anyone want s to see it in action my repo is here.

I know you specified UIImage in your question, but if you're trying to adjust the size of an image displayed on screen, you would do this by modifying the image view that contains the image. In this case you would want to modify the transform property of the image view.
Start by setting the sliders min value to 0.5 and max to 1.5, and linking its value changed control event to an IBAction set up to look something like this. It will expand and contract an image view from the image view's center.
- (IBAction)sliderValueChanged:(UISlider *)sender
{
[_myImageView setTransform:CGAffineTransformMakeScale(sender.value, sender.value)];
}
Additionally, the first post your linked to if for Microsoft WPF and the code couldn't be directly converted without knowing the both relevant API's. Then the second link you provided does show how to directly change the resolution of a UIImage.
You could do something similar to what I've done above with the image view, as in reading the sliders value and formulaically setting the image's size, but I don't recommend that. The slider value changed method will be called very frequently, and may cause performance issues doing this. If you provide more details on what you're trying to do, I may be able to offer different suggestions.

Related

PDFNet Resize annot Swift

Have anyone worked using PDFNet where the annot needs to be resized?
For simplicity, currently i set the text of the annot using
annot.setContents(text)
annot.refreshAppearance()
pdfView.update()
However, if the text is too long where it exceeds the annot's width, it cant be resized and the text will be trimmed as the annot's width isnt wide enough. I checked the documentation and it seems there is a resize function, however the parameter it is taking isn't clear. Anyone with knowledge on this, thanks in advance
How are you creating the (free-text) annotations? If you are using a PTToolManager and creating the annotations with PTFreeTextCreate tool class, then you can enable auto-resize for free-text annotations with toolManager.isAutoResizeFreeTextEnabled = true
Using the PTDocumentController class for viewing and annotating documents is highly recommended if possible, otherwise a PTPDFViewCtrl and PTToolManager should be used.
The PTAnnot.resize() function is used to set the PDF page rect of the annotation, preserving (ie. scaling) the annotation's appearance to fill its new rect.
Auto-resizing a free-text annotation without a PTToolManager would require calculating (yourself) the size of the text. The PTToolManager API approach is recommended.

Determining the "Region" Touched in a UIImageView

I'm trying to determine the region or area of an image that a user touches.
For example, I would like to have a CGRect to define the location of each letter so I can determine when the user touches the "O" in one of the following images…
My first idea was to use locationInView to give me the absolute coordinates and adjust them to the relative size of the ImageView. However, since I'm using AspectFit for the content mode, the relative location of a given region changes with every screen size and orientation because of the image padding on the top/bottom and sides.
In a perfect solution, I would also like to embed this image in a ScrollView so it can be zoomed. But, I can forgo that if necessary.
I don't work with gestures and images very often, so I may be missing something obvious. Any help or ideas you provide will be greatly appreciated.
There is the easy way and the hard way. The easy way it to break this image down into smaller images, one for each letter. The problem with this approach is making them line up as the single image did. If you take this approach you can use CGRectContainsPoint to see which image you tapped. Or you can make each of them buttons or give each image a gesture recognizer to identify what was pressed. If your design is requires you to use a single image then you'll have to make a hitmap describing each location. Each letter will require its own CGMutablePathRef with a CGPathMoveToPoint and a few CGPathAddLineToPoint to describe its shape and location. The method CGPathContainsPoint will tell you which one you hit.

Apply definitely CGAffineTransform* to a UIView

I'm having a problem with scale transformation I have to apply to UIViews on Swift (but it's the same in objective-c too)
I'm applying a CGAffineTransformMakeScale() to multiples views during a gestureRecognizer.
It's like a loop for a cards deck. I remove the one on top and the X others behind scale up and a new one is added in the back.
The first iteration works as expected. But when I try to swipe the new front one, all the cards reset to their initial frame size because i'm trying to apply a new transform, which seems to cancel the previous one and reset the view to its initial state.
How can I apply definitely/commit the first transform change to be able to apply a new one after that based on the UIView resulting new size ?
I tried a UIView.commitAnimations() but no change.
EDIT :
Here's a simple example to understand what I try to do :
Imagine I have an initial UIView of 100x100
I have a shrink factor of 0.95, which means next views behind will be 95x95, then 90.25, then 85.73, etc
If I remove the top one (100x100), I want to scale up the others, so the 95x95 will become 100x100, etc
This is done by applying the inverse of the shrink factor, here 1.052631...
First time I apply the inverse factor, all views are correctly resized.
My problem is, when I trigger again by a swipe on the new front UIView a new resize of all views (So, for example, the 90.25x90.25 which became 95x95 should now scale to 100x100).
At this moment, the same CGAffineTransformMakeScale() is apply to all views, which all instantly reset to their original frame size (so the now 95x95 reset to 90.25x90.25, and then begin to apply the transformation on this old size).
As suggested here or elsewhere, using UIView.commitAnimations() in the end of each transformation don't change anything, and using a CGAffineTransformConcat() is like powering over and over the scaling by himself and of course views become insanely big...
I hope I made myself more clear, that's not easy to explain, don't hesitate to ask if something is wrong here.
After a lot of reading and consulting colleagues who know better than me about iOS programmation, here's my conclusion :
Applying a CGAffineTransformMakeScale() only modify visually a view but not its properties, and since it's difficult (and costly) to modify afterward the bounds and/or frame of a view, I should avoid to try to make a transform, update bounds, make another transform, etc.
Applying the same CGAffineTransformMakeScale() only reset the effect and not apply to the previous one.
Applying a CGAffineTransformScale() with the same values on top of the previous CGAffineTransformMakeScale() (or with a CGAffineTransformConcat()) has some unpredictable effect and will be very difficult to calculate precisely the new values to apply each time to get the effect I want.
The best way I can go with this is only applying one CGAffineTransformMakeScale() that I will keep updating scales values all along the view's life.
It implies now for me to rework all my implementation logic in reverse, but that's the easiest way to do this right.
Thanks all for your tips.

UISlider - draw line / image to the track (bar)

I have UISlider with min and max track colors. I want to draw vertical line (or image) in the middle of the slider track. This line must be there the whole time, only color on top of it will be different (min / max track color).
If I put UIImage on the Slider background, tracks covers it. If I set clear color for tracks, I can see line, but no colors from tracks (obviously). Is there any simple way, how to do this, or I have to override drawRect method for Slider ?
Something like on the slider in image
Note that the sort of "ultimate" solution to this type of project is...
http://www.paintcodeapp.com
As #BradAllred pointed out, the fundamental problem here is: you are trying to avoid drawRect.
Realistically, you can only achieve your goal here by subclassing UIControl or slider.
As others have mentioned fortunately there are many great tutorials, etc, on doing this -- and it's not hard.
Once again paintcodeapp.com is kind of an "incredible secret" to making iOS apps, enjoy.
add an UIView that is 1 pixel wide and has the height of the Slider and put it over the slider.
Create a CALayer, draw it, (even add an image to it), then add that layer as a sublayer to the Slider's layer.contents.

jquery Image slider without define width?

I have used the coin slider.But the coin slider is restricted by the width.I need to run my application in various screen size like 1280*768,800*600.is there any image slider in jquery without restrict width of image?Please help me.
Thanks in advance.
Most image sliders are designed for being placed within a container of a certain width, so that they fit within a specific slot in a layout. Given that the size of the screen/window(you don't specify) is irrelevant to that, it seems like what you're looking for is a gallery that adapts to the size of the entire window, rather than fit within a specific size.
You should probably widen your search to JS galleries in general, which might have that option or even function that way in the first place. As initial suggestions, have a look at the full-screen example for Galleria, or maybe the Supersized plugin

Resources