TapToZoom queries - objective-c

Has anyone analyzed the sample code for TapToZoom on iPhone.
The program performs 3 things
1. Double Tap, perform zoom
2. Two finger tap, perform zoom out.
and 3. Pinch zoom
I don't understand a small part of it.
The real image size is 1730 × 2430
So there is a scroll view in which an UIImageView of size 1730 × 2430 present. The image is set to Scale to Fit. i.e.
if the image is set to scale to fill the UIImageView which also has the 1730 × 2430, how does the image suddenly on launch fit the screen for the iPhone with reloution 320x460
The Image in the xib in its native size.
But when the app starts off, the image is not in its original size, but fits the scrollview content to fit.
How is this accomplished. I didn't find any piece of code that was doing the same in the app.

Check this piece of code is present in viewDidLoad -
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];

Related

Apply Image in Buttons

As A Begineer I've made a Puzzle game and it's working fine in Iphone Simulators.
But problem occurred when I run it in bigger Screen like IPad Air 2.All the picture aren't fitting perfectly in the buttons
(Note that Images are applied on buttons not in the Background of
buttons)
As image size not fit to given size of button, may be due to size concern of aspect fit property of imageview of button. You can try to scale image proportionally to size of imageview of button and get desire result.
You can refer to this link for scale image as proportionally to desired size.
scale Image in an UIButton to AspectFit?

Cocoa - screen capture and draw (retina)

I`m try get screen capture from retina display, and draw this image on window.
I can get capture
CGDirectDisplayID displayId = CGMainDisplayID()
CGImage imageRef = CGDisplayCreateImage(displayId)
image have a size 2560x1600
Now, i need draw this image on window. But if, i resize window to full screen, window return size 1280x800. If i wont draw image on window, i need scale image to 1280x800. Image lost quality, this is not good.
Please help me, how i can get screen capture from retina display and draw his on window, without lost quality ?
You can find out actual window size in pixels by passing its frame to this method:
-[NSWindow convertRectToBacking:]
Just draw the image to the view's bounds. In theory this is a scaling operation, but in reality it's not because the view's bounds are in points, not pixels. The backing buffer for the view is at the same DPI as the original screen capture, so it will be a straight blit. No detail will be lost.

How can I trim a UIImageView to fit an aspect ratio image

I am using a crop tool in my app and I need to modify a UIImageView so that it fits an image exactly after inserting the image in aspect fit mode.
So an image is selected and added to the UIImageView in aspect fit mode. The problem is that this then leaves "blank space" around the image inside the UIImageView that needs trimming. I was wondering how I could then go and resize the holding UIImageView based upon the image inside.
Is this possible?
The Easy way is simply using the following code on your "imageView"
imageView.contentMode = UIViewContentModeScaleAspectFit;
Assuming you want to cut a "zoomed" section of your image to fit fully into your imageView
check your original image width and height
Assuming width is bigger in size then height , scale the image width to the holder width
center the image on your holder , the width will fit perfectly (section2) and the height will simply be cropped follow above and below the holder.
It turns out that a better approach is to use the following idea.
How to get the size of a scaled UIImage in UIImageView?
Instead of trimming the UIImageView, insert the image and then get the dimensions of the image inside the UIImageView, from there you can then resize the UIImageView to match the dimensions of the image inside.

Can a UIViewImage draw its image outside its frame?

When making a photo viewer app found that our UIImageView controller is drawing its image outside its frame when the content mode is different neither ScaleToFill nor Aspect Fit.
Trying to understand why; I isolated the problem making a new project which only has a UIImageView with the following frame (50,50,100,100). The image size contained in it is (4592,3056).
After running the app, with the content mode set to ScaleToFill and AspectFit it all worked as expected:
But after setting the contentMode of the UIImageView to TopLeft, the image is drawn outside its frame, the odd thing is that the log from the frame after all has been drawn is still the original {50,50,100,100}.
I've try to understand the issue by moving the Autoresize, the clips and the content mode of the UIViewController but the result is the same.
set clipToBounds = YES on the view.
its NO by default because that makes drawing way cheaper

Using resizableImageWithCapInsets: image for button only works for the state set, other states show a "gap"

When using resizableImageWithCapInsets: to create an image for a UIButton only the normal state (the state set the image with using setBackgroundImage:forState:) works. All other states show a gap instead of the drawn image. UIButton says that if no image is set for a particular state, the normal state image will be used with an overlay for disabled and selected states.
Here is the normal state:
Here is the selected state:
And here is the source image:
It clearly is using the resizable image I provided, but the image is not drawing the resized area. (You can see the left and right edges but the middle area that is to be stretched just isn't drawn).
Interestingly, stretchableImageWithLeftCapWidth:topCapHeight: does work. Now this is a deprecated method in iOS 5, but with the gap being shown in the new API, I may be stuck using it.
I do recognize that I can provide more images for each state but that defeats the purpose I'm trying to achieve of reducing memory footprint plus adds extra dependency on my graphics designer which I'd like to avoid.
// This is the gist of the code being used
UIImage* image = [UIImage imageNamed:#"button.png"];
UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2);
image = [image resizableImageWithCapInsets:insets];
[self.button setBackgroundImage:image forState:UIControlStateNormal];
// Even doing the following results in the same behaviour
[self.button setBackgroundImage:image forState:UIControlStateSelected];
You aren't creating your insets properly for the image capping. I've reproduced your issue and corrected it by using the correct insets.
With your current code, you are creating caps of half of the image height and width - this leaves you with a "stretchable" area of 0x0 pixels - so you get nothing in the middle.
Why this isn't showing up as wrong in the normal state of the button I'm not sure - perhaps there is some optimisation built in to UIButton to fix things or auto-strectch if you don't supply a stretchable image, and this is not applied to the other states.
The caps are supposed to define the area of the image that must not be stretched. In the case of your button.png image, this is 6 pixels on the left and right sides, and 16 pixels in from the top and bottom. This isn't quite standard, you should tell your graphics designer that (at least for left-right which is the most common stretching) you should only have a 1px area in the centre, however this does not affect the outcome. If you do have a 1px stretchable area then you can standardise your code by deriving the caps from the image size as you have tried to do in your question (each cap is then (image.size.height - 1) / 2 for top/bottom, same but with width for left/right).
To get the correct images on your button, use the following code for creating the stretchable image:
UIEdgeInsets insets = UIEdgeInsetsMake(16, 6, 16, 6);
image = [image resizableImageWithCapInsets:insets];
I was experiencing problems while using resizable images on iOS5 too. It turns out that if your button is of type "RountedRect" and you manipulate the background images, the resizable images will not behave as expected. (iOS6 handles this ok, presumably by assuming your new button type and adjusting as needed.)