is it possible to assign a highres custom uitabbaritem image?
UIImage *img;
img = [UIImage imageNamed:#"TabIcon51#2x.png"];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"more" image:img tag:5];
this doesnt work. is there a workaround, or even better an officel link / solution for this usecase?
thanks
alex
If you're using imageNamed, you can exclude the #2x. The way imageNamed works is that if you're on a high res device it automatically loads the #2x file if it exists, otherwise it loads the regular file.
Related
I have a very weird issue. I have a watch app and I am trying to display an image programatically. When I debug the code, I can see the image is correctly loaded but on simulator or real device I cannot see the image. I have added that image in both watch and extension bundle resources.
Firstly I use the code below to open the view.
NSArray *objects =[NSArray arrayWithObjects:#"MainPageView",#"InterfaceController",#"PoseImageView",nil];
[self presentControllerWithNames:objects contexts:nil];
Inside PoseImageView I am trying to display an image with:
UIImage *imgTest = [UIImage imageNamed:[NSString stringWithFormat:#"1.jpg", strAngle]]; // I can see 1.jpg in debugger
[_imgPose setImage: imgTest];
The image is not there on watch or simulator. I am wondering how to fix the issue.
Note: From assets its not loading as well.
Note2: I can only display the image if the view is initial view.
You are trying to use imageNamed for .jpg which will not work.
Try using
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"imageName"] ofType:#"jpg"];
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
Does anyone has any idea how can I capture screen using objective c in mac os?
to be more specific, how can I capture the active / focused application screen then create an image into a specified path.
Any help is highly appreciated.
#Daniel,
You don't need to understand and implement the whole "Son of Grab". You just need the code below.
The following function will give you the screenshot
// This just invokes the API as you would if you wanted to grab a screen shot. The equivalent using the UI would be to
// enable all windows, turn off "Fit Image Tightly", and then select all windows in the list.
CGImageRef screenShot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
Use the following code to convert it to a NSImage
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:screenShot];
// Create an NSImage and add the bitmap rep to it...
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
bitmapRep = nil;
Have you checked out Apple's “Son of Grab” for capturing images of windows with the CGWindow api?
You can also check Apple's OpenGLScreenSnapshot
I have an image with the following naming convention.
It always shows the non 2x version on retina device.
I had removed cache images from derived data but still not showing.
It works if i explicitly set the imageNamed to "Back"
These are the images.
Back#2x.png
Back.png
UIImage *backImage = [UIImage imageNamed:#"Back"];
NSLog(#"back image height %f",backImage.size.height);
NSLog(#"back image width %f",backImage.size.width);
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setImage:backImage forState:UIControlStateNormal];
btnBack.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[btnBack addTarget:self action:#selector(Click_On_Btn_Back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = backBarButton;
Try
[UIImage imageNamed:#"Back.png"];
Also, make sure both versions of the image are listed under Target->Build Phases->Copy Bundle Resources. If the #2x version is not, make sure when you import it you check the box "Copy into destination group's folder (if needed)."
You should not specify .png in imageNamed: parameter.
Example: files should be names Back.png and Back#2x.png; Your code should be
UIImage *backImage = [UIImage imageNamed:#"Back"];
This is just one approach, but you can get the path from the bundle:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that it not only automatically determines whether to use the #2x version of your image, but also when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.
#Keller's answer should work. are you sure that you are using png images.
I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!
I have a web service producing two versions of graphics; one for Normal Display and another for Retina Display.
Unfortunately I can't add the #2x to the filename since I don't have access to that code.
Is there any way to let the iPhone know that what's loading from the web is a #2x graphic?
Yes there is ... when you load an image resource into an UIImage you can set the scale of that image yourself, ie. tell the iOS whether your image is #2x or not.
This is the code to load the #2x images (in the example from a file, but you can put whatever you want):
[[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path]] CGImage] scale:2.0 orientation:UIImageOrientationUp];
This is the code to load low res images:
[[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path]] CGImage] scale:1.0 orientation:UIImageOrientationUp];
Cheers, Marin