iOS - What is the equivalent of "stretchableImageWithLeftCapWidth:0 topCapHeight:0"? - ios

What is an equal value of
resizableImageWithCapInsets: for stretchableImageWithLeftCapWidth:0 topCapHeight:0?
Also, Is there any simple way to convert the values from stretchableImageWithLeftCapWidth to resizableImageWithCapInsets?
Because the stretchableImageWithLeftCapWidth method was deprecated in iOS 5.
Thanks

I guess your looking for resizableImageWithCapInsets:UIEdgeInsetsZero?

By using:
stretchableImageWithLeftCapWidth:0 topCapHeight:0
You're telling iOS that you want the entire image to be vertically stretchable and you want the entire image to also be horizontally stretchable (itals copied directly from Apple docs). In other words, no part of the image will be preserved unstretched.
In converting your code to use the new method introduced in iOS 5, though, you'll have to keep in mind that the new method works differently than the old one when it determines how to resize the image not contained in the caps (hence the move away from using "stretch" in the method name itself).
While the old method used pure (and not always efficient) scaling of every pixel not contained in the caps as a whole, the new method defaults to the more efficient tiling approach. As a consequence, the results between old and new methods will be very different if the part of the image you're stretching (the part not contained in the caps) measures more than one pixel in the direction(s) being stretched and isn't uniform.
While you have not provided screenshots of the actual image you are working with or the results you're getting, which makes it impossible to tell you exactly why you're not getting the results you want, it does sound like you specifically want your image to be resized with the UIImageResizingModeStretch resizing mode (rather than the default tiling mode). If so, it's likely that you should be using this method instead:
resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:
The answer to your second question also depends on how you want the stretchable part of your image stretched.
Heuristically:
If your old caps were non-zero and the portion being stretched was either 1x1 pixel or at least uniform in the direction(s) of stretching, you'd probably be able to use UIEdgeInsetsMake(*top*, *left*, *bottom*, *right*) with your LeftCap value as left and right and your TopCap value as top and bottom, given that the old method assumed you wanted to stretch the image symmetrically.
If your old caps were 0 or the portion being stretched was larger than 1x1 pixel in any direction(s) of resized and non-uniform, you could still convert the caps in the same way but you'd want to use the method variant that allows you to control the resizingMode.

Related

Get pixel values of image and crop the black part of the image : Objective C

Can I get pixel value of image and crop its black part. For instance, I have the this image:
.
And I want something like this
without the black part.
Any possible solution on how to do this? Any libraries/code?
I am using Objective C.
I have seen this solution to the similar question but I don't understand it in detail. Please kindly provide steps in detail. Thanks.
Probably the fastest way of doing this is iterating through the image and find the border pixels which are not black. Then redraw the image to a new context clipping the rect received by border pixels.
By border pixels I mean the left-most, top-most, bottom-most and right-most. You can find a way to get the raw RGBA buffer from the UIImage through which you may then iterate through width and height and set the border values when appropriate. That means for instance to get leftMostPixel you would first set it to some large value (or to the image width) and then in the iteration if the pixel is not black and if leftMostPixel > x then leftMostPixel = x.
Now that you have the 4 bounding values you can create a frame from it. To redraw just the target rectangle you may use various tools with contexts but probably the easiest is creating the view with size of bounding rect and put an image view with the size of the original image on it and create a screenshot of the view. The image view origin must be minus the origin of the bounded rect though (we put it offscreen a bit).
You may encounter some issues with the orientation of the image though. If the image will have some orientation other then up the raw data will not respect that. So you need to take that into account when creating the bounded rect... Or redraw the image first to make it oriented correctly... Or you can even create a sub buffer with RGBA data and create the CGImage from those data and applying the same orientation to the output UIImage as with input.
So after getting the bounds there are quite a few procedures. Some are slower, some take more memory, some are simply hard to code and have edge cases.

UIImageView - anyway to use 2 content modes at the same time?

So in my scenario, I have a square that is (for understanding's sake) 100x100 and need to display an image that is 300x800 inside of it.
What I want to do is be able to have the image scale just as it would with UIViewContentMode.ScaleAspectFill so that the width scales properly to 100.
However, after that, I would like to then "move" the image up to the top of the image instead of it putting it inside the imageView right in the center, basically what UIViewContentMode.Top does. However that doesn't scale it first.
Is there anyway to do this type of behavior with the built in tools? Anyway to add multiple contentModes?
I already had a helper function that I wrote that scaled an image to a specific size passed in, so I just wrote a function that calculated the scaled image that would fit into the smaller square I had similar to the size AspectFill would do, and then I wrote code that would crop it with the rectangle size I needed at (0,0).

obj-c images resizing automatically...?

I just started learning obj-c the other day and i'm putting together a crappy game as practice. I have an IMG as a main character to the game and every time this character hits certain obstacles, i wanted the size of the image to change (decrease or increase depending on the circumstance).
I used photoshop the change the size of the images to the appropriate sizes but for some reason, when i run the game, the images change when i want them too, but they are way smaller than the size i set them to in photoshop......
any ideas?
i don't think its necessary to post my code for something as simple as this, right? its just a simple "if" statement followed by the instance and the UIimage named for the image name...
I assume you display the image in an UIImageView? Or do you use SpriteKit? If you use an UIImageView the image is actually automatically scaled to the size of the UIImageView.
Therefore you would just change the size of the UIImageView (you can of course also change the image inside the UIImageView.
If you are using SpriteKit, you have to remember, that your are probably testing on a 'retina' device and for this reason the image's width and height is divided by 2 (the real resolution of the current iPhone 5/5s is 1136x640 and not 568x320 !

Trim UIImageView to fit content

Does anyone know how I could trim a UIImageView of some image with surrounding transparency down to just barely fit the content by cropping off the edges?
You can try using this category:
https://github.com/Clstroud/UIImage-Trim
Here is a usage example from their docs:
UIImage-Trim Category for trimming transparent pixels of an UIImage
object.
How to use
Add the UIImage+Trim files to your project. Include UIImage+Trim.h in
the files where you want to trim your images.
Trimming is pretty straightforward:
[yourImage imageByTrimmingTransparentPixels];
Optionally, you may want to consider any non-opaque pixels as being
transparent (for instance, cropping out a light drop shadow). This can
be achieved by using the alternate method:
[yourImage imageByTrimmingTransparentPixelsRequiringFullOpacity:YES];
Additionally, if you merely desire to know the UIEdgeInsets of the
transparency around the image, you may want to use the following:
[yourImage transparencyInsetsRequiringFullOpacity:YES];
This call works based on the same principles as the "advanced" trim
method, with the boolean dictating whether non-opaque pixels should be
considered transparent.

Resize image without stretching interior

I often use UIimage's resizeableImageWithCapInsets method. However, I would now like to do the reverse i.e. resize the areas covered by the cap and leave the uncapped area unstretched. Is this possible?

Resources