How to change image with device language? Xcode - ios

I have a background image that has some texts too. So I want to change it with the device language. But the background changes with if statement in my code. So I need to change the image itself to optimize it for another language. How can I make this happen?

You can do this by creating a UIImage with the "imageNamed" constructor, but use an NSLocalizedString for the image name:
UIImage* localizeImg = [UIImage imageNamed:NSLocalizedString(#"imageNameKey", nil)];
Then in your localized .strings files you just put the name of the image that you want tied to that language.
"imageNameKey" = "englishBackground.png";
Apply the newly created UIImage to the background and voila! Hope that helps!

Related

Programatically Set UIImage Animation with WatchKit

I can't seem to figure out how to programmatically set a new image, via the outlet, and make it start animating.
Sequence
zeroEntering0.png
zeroEntering1.png
zeroEntering2.png
zeroEntering3.png
zeroEntering4.png
I imported the sequence of images into the Image.xcassets inside the WatchKit App
I can set the image in the interface builder to "zeroEntering" and set animating to "Yes" and it works correctly.
However, I want something more dynamic, I need a button press to choose a new animation sequence and start it off. If I try and set the image programmatically using the same name from the interface builder, the UIImage is nil.
What naming convention should I use when programmatically setting the UIImage? "zeroEntering", "zeroEntering0", "zeroEntering.png" or "zeroEntering0.png"
I tried using the two non-nil options and the image did not animate and went black.
The answer is subtle and definitely got my wheels spinning for too long.
According to this beautiful article,
You should use setImageNamed(:) when the image you want to display is either cached on the watch on is in an asset catalog in the watch app’s bundle, and use setImage(:) when the image isn’t cached — this will transfer the image data to the Apple Watch over the air!
So, I kept my images in the assets catalog on the watch app, and switch to use,
[self.testImage setImageNamed:#"zeroEntering"];
[self.testImage startAnimatingWithImagesInRange:NSMakeRange(0, 4) duration:0.2 repeatCount:100];
Set the image as [UIImage imageNamed:#"entering"] then call startAnimatingWithImagesInRange:duration:repeatCount:
Check it out here: https://developer.apple.com/library/prerelease/ios/documentation/WatchKit/Reference/WKInterfaceImage_class/#//apple_ref/occ/instm/WKInterfaceImage/startAnimatingWithImagesInRange:duration:repeatCount:
Make sure to follow the tips here: https://developer.apple.com/watchkit/tips/

Use high definition tab bar icons programmatically?

I was wondering how I should go about using high definition tab bar icons if my tab bar icons are being set programmatically. I'm pretty much just doing the standard:
tabBarItem2.image = [UIImage imageNamed:#"ExploreIcon.png"];
Will putting two images in my supporting files named "ExploreIcon.png" at 30x30 pixels and "ExploreIcon#2x.png" work without using additional code? Thanks for the help!
You don't need to specify the retina version, the system will use the higher resolution version if present in the bundle. Also you don't need to add the extension
[UIImage imageNamed:#"ExploreIcon"];
the two images should have te same name for example :
ExploreIcon.png
ExploreIcon#2x.png
You need not consider choosing file which fits and displays better on a specified device, Let iOS do that for you.
Just keep files to support all possible devices.
Follow:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
tabBarItem2.image = [UIImage imageNamed:#"ExploreIcon.png"];
This is good enough. Just make sure the file names are appropriate. i.e.
you have to add an imageview and set the cgrect size 40*40 and than you will add that image view on the tab bar icon image.

UIImage looks different when used in iOS

I am having problems with a png image, that gets wrong colors on iOS compared to the actual image.
It does not matter how I am using the image, it always gets the wrong colors. I have tried on UIButton and UIImageView and it gives the same result.
It is a very standard use of a UIImage:
UIImage* greenButtonImg = [UIImage imageNamed:#"btn_green"];
UIImageView* testView = [[UIImageView alloc] initWithImage:greenButtonImg];
[self.view addSubview:testView];
The second image is how it looks on iOS and the first button is how it looks on my Mac (Finder and Photoshop):
As you can see the second button has a different green color.
This is happening all over the app where am using this picture. It happens in the Simulator and on a iPhone 5.
What can cause this issue? Can this be caused by settings in Photoshop, where the image was created?
As Jeff wrote in a comment it was a problem with the RGB Profiles.
I managed to fix the problem by converting the color profile in Photoshop:
Edit -> Convert to Profile... -> Set profile to "Apple RGB"
In iOS 7.0, the image is colored with the toolbar’s tintColor.
In iOS 7.0, all subclasses of UIView derive their behavior for tintColor from the base class.
By default, an image (UIImage) is created with UIImageRenderingModeAutomatic.
If you have UIImageRenderingModeAutomatic set on your image, it will be treated as template or original based on its context.
Certain UIKit elements—including navigation bars, tab bars, toolbars, segmented controls—automatically treat their foreground images as templates, although their background images are treated as original.
Other elements—such as image views and web views—treat their images as originals. If you want your image to always be treated as a template regardless of context, set UIImageRenderingModeAlwaysTemplate.
If you want your image to always be treated as original, set UIImageRenderingModeAlwaysOriginal.
Refer Template Images for more info.

Picture doesn't show on iPhone when running app

I have set the backgroundColor to a picture which is located in the project folder. When I run the app in the simulator the pic is shown. But when running on my real iPhone it's just white. What can be causing this? Could it be because I have <0.5gb free on both the computer and iPhone?
I had similar situation.
It seems the simulator - Xcode is cacheing the image somewhere, but at build time is loosing.
Try: uninstall the app from simulator, device.
Clean the project.
Force close XCode, not just the project, close it.
Open, clean project, build
At this step should match the device and simulator behaviour, hopefully it is there the desired image.
Please check spelling of the image name you used. In actual devices it's case sensitive.
That should be plenty of free memory. When adding the file to your project, and make sure you choose the options for the image to be copied into the project and to be part of the target.
EDIT: my above suggestion is only useful if the UIImageView you are trying to load points to a picture, and I guess in this case you are just using a blank UIImageView and setting background color.
Also, you might want to look at the arrangement (ie. send to back, send to front, etc. Try sending to front in case there was something in front of it blocking the view) I think these options are in the menu bar under Design --> Arrangement
Could you post some code?
You'll want to add the image not as a backgroundColor, but as a UIImageView subview.
// load the image
UIImage *image = [UIImage imageNamed:#"image.png"];
// create the image view using the image
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
// add the image view as a subview
[yourview addSubview:imageView];
First try to Uninstall the app Clean the project then Install and then check if not then.
Second there may some issue with picture spelling Case senstive or Not As The iPhone is case sensitive, but the simulator is not because iPhone file system is case sensitive and Mac OS is not
Third
Add a break point where you add UIImage
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"a_image.png"];
Then on the console "po [imgView image]"
If it is nil then either the resource is not getting copied correctly into the bundle.

Custom button not appearing on iOS device

So I'm trying to implement a custom button in my iOS app to replace the default rounded rectangle button. Heres a code snippet:
UIImage *normalImage = [[UIImage imageNamed:#"Images/whitebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
UIImage *pressedImage = [[UIImage imageNamed:#"Images/bluebutton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[self.theButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[self.theButton setBackgroundImage:pressedImage forState:UIControlStateHighlighted];
When I run it in the simulator everything works fine, my custom button shows up. However, when I run it on my actual device the button just appears as the default one with no customisation at all. Anybody got any idea where I'm going wrong?
The iOS file system is case sensitive, unlike the sinulator's. I'd start by checking that "Images" directory.
(Quick test if I'm barking up the wrong tree: does the +imageNamed: call return nil?)
You should add whitebutton.png and bluebutton.png to your project. And call imageNamed:#"filename" not imageNamed:#"file path" . Also , when running on the simulator there is no problem with case sensitivity. On the device , you have to type the correct name of the file because it IS case sensitive.
Hope this helps.
Cheers,
George
Your resources are going to get flattened in your app bundle. Also, the filesystem on iOS is case sensitive while the filesystem on Mac OS X is case preserving, so make sure the actual filenames of your images are case-correct. If they are, just remove the Images/ prefix and it should work:
[UIImage imageNamed:#"bluebutton"];
Also, if the image is a png type, you don't need to specify the file extension with imageNamed:.
Use
[UIImage imageNamed:#"whitebutton.png"]
and
[UIImage imageNamed:#"bluebutton.png"]
Regardless of how you have it visually organized in Xcode, it all gets flattened in the bundle

Resources