Why is [UIImage imageNamed] displaying a blue rectangle instead of my image? - objective-c

I'm trying to use an image for a button in the Nav Bar. It appears to be finding the image, because when I run the app, there is blue rectangle roughly the size of my image centered on where the nav bar button is supposed to be. And when I click it, the action specified for the button happens. But why is the image not displayed? Here's my code:
UIImage *image = [UIImage imageNamed:#"newTweetSmall"];
[self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:#selector(newTweet)];
I've also tried
UIImage *image = [UIImage imageWithContentsOfFile:#"newTweetSmall"];
But that appears to not be finding the image at all, because there is no big blue rectangle, no button in the Nav Bar at all.
Here's the structure of my project, in case that matters. I've also tried using 'Resources/newTweetSmall' for the path of the image, but that results in no image or button either.

Your code is correct. Try use a png image with alpha channel.
It certainly solve your problem.
If you want to be convinced that your file was loaded to memory check the value in the image variable. If image is loaded this variable cannot be nil.

Related

Print view larger than screen to PDF

I can print a nice perfect Report on iPad, but now we want to port our app to iPhones, too.
All normal views are being printed as they have to, except the one view where we use ShinobiCharts (OpenGL): on iPhone only the screensize will be printed, the rest of the PDF-Sheet will remain white.
I tried putting it into a scrollview and programatically assign the right resolution to the view before it was printed, but this resulted in the small window being streched to fit the PDF-Sheet's size, still not displaying the whole diagrams.
Normal views:
UIGraphicsBeginPDFPage();
[pdf1.view.layer renderInContext:pdfContext];
Diagram view:
UIGraphicsBeginPDFPage();
UIGraphicsBeginImageContextWithOptions(pdf8.view.bounds.size, NO, 0.0);
[pdf8.view drawViewHierarchyInRect:pdf8.view.bounds afterScreenUpdates:YES];
UIImage *pdf8Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *pdf8ImageView = [[UIImageView alloc] initWithImage:pdf8Image];
[pdf8ImageView.layer renderInContext:pdfContext];
Full image on iPad
Cropped image without ScrollView
Scaled image with ScrollView

set both backIndicatorImage and backIndicatorTransitionMaskImage on backbarbutton in IOS7 didnot show particular image

I searched all the topics about backbarbutton custom, but seems failed to find answers about my problem on backbarbutton.
To keep the property called interactivePopGestureRecognizer of UINavigationController, using leftBarButton to replace backBarButton is not a valid solution. But I want to custom the backbarbutton with a image, so I use the following code in my controller ViewDidLoad:
UIBarButtonItem * btnBack = [[UIBarButtonItem alloc]init];
btnBack.title = #"";
self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:#"backBtn"];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:#"backBtn"];
self.navigationItem.backBarButtonItem = btnBack;
when I run my project, it shows just a blue rectangle in the place where backBarButton always show.
But the original image just like the system barbutton that called reply.
However, it has a different effect, just change to the image named "Drinks" which is a black image that show a glass of juice.
after run my project the place where always show backBarButton show a blue image just like "Drinks".
what happend! How can I custom my backBarButton with the giving image named backBtn.png, is there anyone can help me? Thanks in advance! For image submit has been reject, I discrible my problem in words.
The Image has be transparent where there should be nothing to be drawn. It should be opaque where the content of the image will be drawn. At least that worked for me. The final color will be tinted by the navigationbar's tintColor anyway.

How to properly position the back button in iOS7

I used this code to use a custom image as the back button in the whole app.
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"back"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"back"]];
The image dimensions are 30 x 30.
The code adds the image as the back button but the position is not the correct, as you can see in the following image:
Any ideas on how to properly position the image without modifying its dimensions (at least the visual part of the image (circle + arrow))?
EDIT:
I don't want to use a custom back button because that forces me to disable the swipe/back-gesture in iOS7
EDIT
I think I might have found the trick (in iOS 7 Design Resource -- UIKit User Interface Catalog.)
Under Bar Button Items
Note that a bar button image will be automatically rendered as a template image within a navigation bar, unless you explicitly set its rendering mode to UIImageRenderingModeAlwaysOriginal. For more information, see Template Images.
Under Template Images they have some code to specify the UIImageRenderingMode.
UIImage *myImage = [UIImage imageNamed:#"back"];
UIImage *backButtonImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// now use the new backButtomImage
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
Try creating the UIImage with alignment insets and then set the Back Indicator image.
UIEdgeInsets insets = UIEdgeInsetsMake(10, 0, 0, 0); // or (0, 0, -10.0, 0)
UIImage *alignedImage = [[UIImage imageNamed:#"back"] imageWithAlignmentRectInsets:insets];
[[UINavigationBar appearance] setBackIndicatorImage:alignedImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:alignedImage];
You might also try adjusting the position of the UINavigationBar title text
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics];
Well just follow one of the suggestions to fix the layout and lose the iOS 7 "back gesture", and then fix it with a UIScreenEdgePanGestureRecognizer!
A UIScreenEdgePanGestureRecognizer looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.
PLEASE SEE EDIT BELOW!!!
I created a custom back button in iOS7 not too long ago. Mine has an arrow and the word back on it. I do think pawan's suggestion is a good start. To create the back button with your custom image you can use,
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked)];
[backButton setBackgroundImage:finalImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[backButton setTitlePositionAdjustment:UIOffsetMake(-20, 0) forBarMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = backButton;
My image finalImage is a composite of two different images, but you can just use your "back" image. But I think that is where the problem lies. My image was a composite, you might want to make a composite as well, but put a clear space above your back icon. I placed a clear space to the right of my icon to adjust it's spacing. Here is the code,
UIImage *arrow = [UIImage imageNamed:#"back.png"];
UIImage *wordSpace = [UIImage imageNamed:#"whiteSpace.png"];
CGSize size = CGSizeMake(arrow.size.width + wordSpace.size.width, arrow.size.height);
UIGraphicsBeginImageContext(size);
[arrow drawInRect:CGRectMake(0, 0, arrow.size.width, size.height)];
[wordSpace drawInRect:CGRectMake(arrow.size.width, 0, wordSpace.size.width, wordSpace.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The Image wordSpace is a clear png that I made in photoshop so my new back button image was not stretched. You might want to place a clear png on top, to push the icon down a little. Make the size.height of it in photoshop for what you think the adjustment should be. You might need to futz with this a bit. And make sure to change up the CGSize so that it fits your icon and the clear space.
My word back was a bit off, so I looked at
[backButton setTitlePositionAdjustment:UIOffsetMake(-20, 0) forBarMetrics:UIBarMetricsDefault];
I had to play around with that line a bit to make it look as good as possible but it finally gave me what I wanted with the -20. I even adjusted the second variable which is 0 in mine, this moved the actual icon around. -5 put the icon down way to far, but its another option from the clear png.
Now to deal with the fact that you want it to be an actual back button. Look at the first line of code I posted. The action on the button is #selector(backButtonClicked). So all you need to do is make that method and you should be good to go!
- (void)backButtonClicked
{
NSLog(#"going back");
[self.navigationController popViewControllerAnimated:YES];
}
Hope this helps a bit.
EDIT*****
I was playing around with my code a little bit and found a better way to move the back icon. I just used a ship's wheel because I didn't have the same one that you did, but it will work the same.
Since you don't really want a title you can create the button with this code,
UIImage *image = [UIImage imageNamed:#"781-ships-wheel.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked)];
Just change the 781 stuff with your icon's name. Then you can move it around with the following,
[backButton setImageInsets:UIEdgeInsetsMake(20, 0, -20, 0)];
Take a look at this picture.
This shows the icon down considerably, but I wanted to show you the idea. The numbers for the Edge insets are Top, Left, Bottom, and Right. Don't touch the left and right if you don't need to move it that way, change the top and bottom. Notice however, that if you need to move it down by 20 points like I did, (way too much) you need to offset in the negative for the bottom, or the icon will get compressed. This is what it looks like with all zero's.
So you can pretty much move it where ever you want, but you will still have to set up the #selector(backButtonClicked) to make it work like the real back button.
This is Swift 2 version.
The simplest way is like this. Put this code in AppDelegate.'
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationBarAppearace = UINavigationBar.appearance()
let image = UIImage(named: "back-btn")
navigationBarAppearace.backIndicatorImage = image
navigationBarAppearace.backIndicatorTransitionMaskImage = image
return true
}
if your back button has background colour, it may won't work correctly.
Add your icon to asset folder for each resolution like this:
You can try this
self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 10, 0);
The problem is that your image is too tall. To prove this, first try this code:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,20), NO, 0);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(6,0,4,20));
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.navbar.backIndicatorImage = im;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,20), NO, 0);
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.navbar.backIndicatorTransitionMaskImage = im2;
It looks fine. Now change the 20 to 30 in the two CGSizeMake calls:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,30), NO, 0);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(6,0,4,20));
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.navbar.backIndicatorImage = im;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,30), NO, 0);
UIImage* im2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.navbar.backIndicatorTransitionMaskImage = im2;
The icon is now too high.
So just make your image 20 pixels tall and all will be well.
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, -2, 0); // or (2,0,0,0)
UIImage *backArrowImage = [[UIImage imageNamed:#"back"] imageWithAlignmentRectInsets:insets];
[[UINavigationBar appearance] setBackIndicatorImage:backArrowImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backArrowImage];

UINavigationBar Back Button stretchable image too long

I am using the following code to customise the back button of our UINavigationBar. However, the image is stretched too far, resulting in an image like the following. Please can you tell me how to prevent this?
Thanks!
[[UIImage imageNamed:#"back_button"] stretchableImageWithLeftCapWidth:14 topCapHeight:0]
Original Back Button Image:
The minimum width of the UIBarButtonItem is largely determined by the size of the image you provide. You should export your back arrow image from your editor so that there is only 1 pixel of tileable image content in the middle column of the image, such that the left and right portions can be used as the left and right caps:
As per the image, it seems image is stretched correctly. Just check the back button frame. Also please verify, if there is no whitespace in end of the string "Profile".
stretchableImageWithLeftCapWidth:topCapHeight is deprecated in iOS 5.
This doesn't answer your question. It's just a hint.
Deprecated UIImage Methods
I have an image with a noise texture on it and wanted to do the same thing. I finally arrived at this solution which I believe does exactly what you want (at least in iOS 6):
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
UIImage *buttonBg = [[UIImage imageNamed:#"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 6)];
[backButton setBackButtonBackgroundImage:buttonBg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
backButton.title = #"Back";
You can customize your edge insets to exclude portions that shouldn't stretch.

How to add image to navigation back button

I have a backbutton image (backButton). How can I add it to the back button of the navigation bar?
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"backButton"]
style:UIBarButtonItemStylePlain target:nil action:nil];
viewController.navigationItem.backBarButtonItem = newBackButton;
[newBackButton release];
However, I am getting a weird looking button as seen below.
How can I replace the backbutton with the full black back button image?
You'll have to modify the black image's pixel size. Open up the image in any image editor, go to the adjust image size (it may be called something else in a different photo editor) find out the size and pixels. It looks like you'll have to make the thing 1 pixel taller and like 2 pixels wider, but I'm not sure. You can probably find some documentation on the exactly pixel size of the navigation button and work with that. Goodluck