How to change image mode of WKInterfaceImage programmatically ?(objective C) - ios

I am developing an app for watchkit2. I am setting image mode of WKInterfaceImage object from the interface builder to centre, I need to change it to Aspect Fit after some code is executed.
I cannot see any mention of doing this via code in apple's documentation..
Has anyone encountered this scenario yet ?

Try to add 2 instances of your image. Set first's image mode to center, and second's to Aspect Fit. But hide second (hidden flag).
You can show and hide your images with setHidden method

Related

How to add a gradient background with LaunchScreen.storyboard?

I want to add a gradient background to LaunchScreen.storyboard.
Since this gradient changes with the diagonal of the screen, it is difficult for UIImageView to support screens with different aspect ratios.
So far, Launch Image was used to solve this.
However, Apple enforces the use of LaunchScreen.storyboard.
How do I add this to LaunchScreen.storyboard?
Thanks.
Use an image editor (or whatever suits you) to create that as an image.
You can then add a UIImageView to your LaunchScreen.storyboard (Content Mode: Scale to Fill).
Edit
LaunchScreen storyboards cannot execute any code - and that includes trying to use a custom subclass.
If scaling a single image doesn't suit your needs, you do have a (limited) option.
UIImageView - including when used in a LaunchScreen - can have different images for different size traits:
You cannot get down to actual screen sizes, but using different images for the different trait combinations might do the trick.

Xamarin iOS - loading a button image from code

I have the following Image Set defined in my Media.xcassets library...
I am loading this image to a button as follows...
public override void ViewDidLoad()
{
base.ViewDidLoad();
FSDirectButton.SetImage(UIImage.FromBundle("FSDirectButton"), UIControlState.Normal);
}
However, the button is not the correct size. How do I load an image to a button and then have the button resize to fit the image? I am not sure which image is being picked (1x, 2x, 3x).
Here is a recipe that I have used in the past that has helped greatly: https://developer.xamarin.com/recipes/ios/standard_controls/buttons/use_an_image_for_a_button/
a few things you may also want to consider trying:
1. Make sure constraints are set so that the button is always the right size
2. If all else fails try adding the images to your resource folder and accessing them as files instead as a temporary workaround.
3. also, you may want to try SetBackgroundImage(), instead of SetImage to see if it yields different results.
4. Try setting the image for all the control states of the button (highlighted, disabled, etc.)
5. try changing the content mode for the imageview inside the button. It may just be that the image is too large and doesn't scale to fit the alloted space. access this by using FSDirectButton.ImageView.ContentMode
To answer your question about which image size is selected (1x, 2x, etc.), I believe that is determined by the resolution of the IOS device that the image is being displayed on. that way if someone uses an iphone or an iPad the image will still look nice and clear without you having to write a lot more code to choose the best image for each device.
How do I load an image to a button and then have the button resize to
fit the image?
If you want your button auto resizing to fit its image. Please use Autolayout to layout your button.
Try to add the top constraint and left constraint to define the element's position in X and Y. Then the button will auto adjust its size depending on its content if we don't add the width and height constraint to the button.
I am not sure which image is being picked (1x, 2x, 3x).
After adding the images, the system will decide which image should be picked depending on which device the app run on. we just need to use the image name without the suffix. More details about which device picks which type of image you can refer to this

Position image without autolayout

I hope this question makes sense.
I have 2 images in my viewcontroller. One is for a background and the second I need to move around on top of that image freely. I am using autolayout for the background image, but when I test on different devices the second image is not in the proper place because I am not using the autolayout on it and therefore it does not move correctly.
How can I move the image to be in the proper place. Is there some way to set it properly in code. I guess I mean how could I set the autolayout in code since this image will have many positions it needs to move to.

How to create rounded images with WatchKit?

I'm trying to do a rounded images to be shown in my WatchKit application but I don't find any method to do it programatically.
What I have done to perform that, it is use my original image as a group background and put inside a mask image with rounded shape but I think this is not so clean.
Another different solution that I have thought is to create in my backend a new thumbnail or similar with the shape I want to show, but this require more time to be programed.
I could use also radius parameter from WKInterfaceGroup but, in my case (see the image below), I will lose my notification bubble that I have in a corner.
My app interface:
Uber application example:
Put your WKInterfaceImage inside a WKInterfaceGroup and round the corners of the group in Interface Builder.
What you are trying to create is not possible at the moment (we'll see if Apple will add new features to WatchKit this Summer). You'll need to mask your icons on your device and cache on your Watch.
As 'bgilham' said you could put the WKInterfaceImage inside WKInterfaceGroup, but you don't have to. You can just set the background image of WKInterfaceGroup. This time you can create some shiny fake subviews. WKInterfaceGroup inside WKInterfaceGroup with different background images on them.
For example. Lets say you have three ui (WKInterface) elements 'iconGroup', 'badgeGroup', 'badgeLabel'.
Center badgeLabel inside badgeGroup. Set badgeGroups radius to half of its height. If badgeGroups width is equal to its height you'll get a nice circle. Place your badgeGroup inside iconGroup at the top right corner. You can now set the radius of iconGroup equal to the radius of badgeGroup. Set the background image and you'll get a nice rounded rectangle with an dynamic badge.
You also can hide some items (groups) and fake some dynamic changes inside your watch app. There is tons you can create with such fakes. If you will add some animations (for example .png files) you can create fake views with transparent overlays and so on.
UPDATE:
I checked the uber screenshots of uber app, because I was wondering how they created that overlay over the map. My only clue is that they creating a lot of images on their iPhone and sending them to the apple watch to animate the background image of the WKInterfaceGroup. If the images are cached they will play 30fps on the watch otherwise its going to be 10fps or so.
My statement:
So rethink your design, its not always good to make a design like everyone else do. Create something shiny and new so people will memorize your work. :)
Hope this will help you guys out there to rethink your work and create some cool watch apps. Day one apps aren't that great at all, only a few of them are.
You can actually achieve this by using three groups:
The 1st WKInterfaceGroup has your original non-cropped image as its background.
The 2nd WKInterfaceGroup, which is inside the 1st one but has the same size, has the circle image mask as its background.
The 3rd WKInterfaceGroup, which is inside the 2nd one, contains your badge. You can put the text label in it.
Although this requires a pure simple background so the mask image can simply just overlay above the background image, this one doesn't require any image data transferring between the phone and the watch.
The only way to do this right now is just as you said. To mask the profile image into a circle, add it to a group, then add the notification bubble group as a sub-group of the main one (with a circular radius and the label inside.)
Right now there is no way to have a sub-group ignore its parent's clipping radius.
i have used UIImage category from http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/.
this has a Category for Rounded corner images, worked for me in watchkit app.

How to avoid distort and stretch image in Custom UIButton while using vector image set?

Below is my storyboard, I had quite a lot of constrains to make those four buttons fit into the screen nicely. However, after I put the images into the buttons and check the storyboard preview, all images are distorted. I try to adjust (had tried all of the options) the view mode at attribute inspector panel but seems no effect at all.
One more thing, I am using PDF as vector type image set, not regular .png file I am wonder if that matters? Should I change the way vector image displayed inside UIButton programaticly?
The verctor image size was 500px*500px. I resize the image into 50px*50px after #orkenstein 's reminader, xcode may not be able to handle oversized vector image that well. The image appear inside button as it is exactly 50px*50px and lost its ability to scale. Also as he sugested I should but the vector image inside an UIImageView and after my test, it works perfectly fine in an UIImageView.
Setting the contentMode on the button itself doesn't work for me, but setting it on the button's imageView does. From this Stackoverflow answer:
button.imageView?.contentMode = .scaleAspectFit

Resources