UIImageView Changing Issue IPhone Issue - iphone

I am trying to make a button in my IPhone app which changes an existing image (simple, I know! but I've spent 2 days on this :)
I have a button:
- (IBAction)myButton {
myUIImageView.image = [UIImage imageNamed:#"image.jpg"];
NSLog(#"my button");
}
I am attempting to find out why the button is not doing anything. "my button" does get logged to the console so I know its mapped correctly. Firstly, I was hoping someone could tell me if I could perhaps log some more important info here, like the image file path? Or perhaps you see why this isn't working.
I synthesize myUIImageView above this:
#synthesize myUIImageView;
And I declare it in my .h file:
- (IBAction)myButton;
#property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;

Typically, something is nil here.
You have
myUIImageView.image = [UIImage imageNamed:#"image.jpg"];
So the obvious suspects would be either:
myUIImageView is nil, or
[UIImage imagedNamed:#"image.jpg"] is returning nil.
Add some logging to see if either of those is true.
NSLog(#"imageView: %#, image resource: %#", myUIImageView, [UIImage imageNamed:#"image.jpg"]);
If the image isn't being loaded, then I would expect for you to see a change in the UI, so that's probably not the issue.
More likely is that the nib outlet for myUIImageView haven't been wired up correctly in Interface Builder, but the logging should point you in the right direction.

Two things to try:
Make sure that your UIImageView is
actually being displayed. Do do this, just set its background color to something
visible, e.g.,
myUIImageView.backgroundColor =
[UIColor redColor];.
Make sure that your image file
("image.jpg") is part of your
project.

Related

Issues setting up a back ground image and with UIImageView

On my iPhone app, I simply want to set a particular background image, which depends on whether it's an iPhone 5 or not.
So, I tried two approaches:
A) Using
self.view.backgroundColor = [UIColor colorWithPatternImage:backGroundimage];
B) Creating an UIImageView and setting up the image there. Code:
UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:screenBounds];
[backgroundImageView setImage:[UIImage imageNamed:backGroundImage]];
[self.view addSubview:backgroundImageView];
But I am having issues with both of them:
Issues with Step A:
When I set the image through that way, I have to deal with the image scaling issues for different sizes of the screen. I use the following code to do the scalling:
UIGraphicsBeginImageContext(screenBounds.size);
[[UIImage imageNamed:backGroundImage] drawInRect:screenBounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
Another issue from Step A is that the image appears quite blurry. It doesn't have the same sharpness to it.
Issues with Step B:
With this, the image looks really crisp and sharp - just the way it should look.
But when I switch to another view using the following code, strangely enough the UIImageView backgroundImageView still appears on the second one. The code I use to switch views is:
[self presentViewController:secondViewController animated:YES completion:nil];
I even tried [backgroundImageView removeFromSuperview], but that doesn't solve anything either.
So what am I doing wrong? And how can I set up a picture as my background which is dependent on the size of the iphone?
Plan B is a good plan. Presenting another view controller should and will definitely hide your image view. If it isn't happening, then it's a problem with the creation of secondViewController, unrelated to the background image on the presenting VC.
Before presenting secondViewController, log it:
NSLog(#"presenting %#", secondViewController);
I'll bet a dollar that it's nil. If I'm right, let's have a look at how you initialize secondViewController. That's the problem, unrelated to the background image view.
Okay, I finally fixed this issue, although the cause of this issue is still puzzling to me.
To fix this, I had to create an IBOutlet property for UIImageView and hook it up on the XIB file.
The reason I was programmatically creating the UIImageView is because the size of the UIImageView depends on what size iPhone they are using. But for the IBOutlet (let's call it as UIImageViewOutlet, I simply used [self.UIImageViewOutlet setFrame:] to get the size and location that I wanted.
I also discovered that one of the buttons that I was programmatically creating, was still visible in the secondViewController. I ended up creating an Outlet on the XIB file for that one as well and used setFrame on it to position it properly.
If anyone who knows the reason of this problem, I will be very grateful.

How to change the backgroundimage of a button programmaticaly when touching?

I have an IPhone application in which I am using an IBOutlet button with a background. On its click, I call a webservice and on the success of that, I need to change the background of the button. Now I am doing it in this way but nothing's happening:
UIImage *buttonImage = [UIImage imageNamed:#"follow_shop.png"];
[following setBackgroundImage:buttonImage forState:UIControlStateNormal];
Does anybody know how to achieve this?
What you have done in code is correct. You can do three things now (in decreasing order of probability of finding the issue):
1) Check in your IB if initially, you have set its image or backgroundImage. The two are different and image comes above the backgroundImage(effectively rendering backgroundImage as being hidden). Both of these (the image you set in IB and in code) should be either one (image or backgroundImage).
2) Check if follow_shop.png is the correct name. You can also use breakpoints to see if buttonImage has memory or not.
3) Check if following is connected in your IB.
Have an IBOutlet for the button and in your IBAction put this code
UIImage *buttonImage = [UIImage imageNamed:#"yourimage.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateSelected];
Look if your IBOutlet is connected properly (e.g. using NSLog(#"%#",following);)
Re-Add your image (e.g. using Copy files into destination groups folder)
Have a look at your target. Is your image in Copy Bundle Resources?

sdwebimage: Image in table cell gets stretched after initial load

I am using SDWebImage in my iPhone project and I use it to load images to my table cell.
The original downloaded image 150 * 150.
My placeholder image is 32*32 (required size)
First time, when the image loads, sdwebimage does a great job of re-sizing the downloaded image to match the size of the placeholder image (32*32). All good.
However, when I navigate back and then come back to the same page, the image gets stretched and fills the entire cell.imageView height. Is this normal behavior?
I need it to always retain the initial 32*32 size. Can you suggest how to do that?
EDIT: I found this link where another user faces a similar issue (question open). The user also made a short youtube video to explain the issue.
Thoughts?
try that for specific size at the cellForRowAtIndexPath :
UIImageView *addImage = [[UIImageView alloc] init];
[addImage setImageWithURL:[NSURL URLWithString:yourCellImageURL] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[addImage setFrame:CGRectMake(7, 3, 78, 57)];
[cell.contentView addSubview:addImage];
[cell setIndentationLevel:8];
tldr: If your custom UITableViewCell subclass has a #property IBOutlet UIImageView* called imageView, change your name to something else (such as customImageView) and it will work properly. Read the rest of the answer for why
I see nobody has answered that, and I wasted a whole week trying to find the solution to this, so here's what happened to me, and how to prevent it happening to you (thus fixing your problem).
I created a CustomTableViewCell which extends UITableViewCell.
I added #property (weak, nonatomic) IBOutlet UIImageView *imageView; to my custom cell.
Everything seems fine until now, right?
Except it is not.
UITableViewCell already has an imageView property. iOS 7 uses your property at first, but then when the Cell is being reused, the UITableViewCell's imageView is used, therefore ignoring all of your constraints or previously defined fixed sizes.
iOS 8 always uses the image from UITableViewCell. so the problem is more obvious there. Previous iOS versions also had varying degrees of this problem, usually presenting very similar symptoms.
The solution? Either deal with the property UITableViewCell.imageView by yourself (add code for laying out the view) or create your own property with a different name, with which you can add constraints (in case you're using auto layout) on Storyboards or xib.
Try to set proper content mode of the UIImageView
if you want to change image size in SDWebImage.
use this call.
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { /*here you can change size of image */ }];

Customized UINavigationbar in Universal App

I have a Universal App in which I customize my UINavigationBar.
In my iPhone AppDelegate I use this to achieve it:
#implementation UINavigationBar (CustomImage)
static NSMutableDictionary *navigationBarImages = NULL;
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"drawing navbar2");
UIImage *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=[UIImage imageNamed:#"bg_titleBar.png"];
UIImage *image = imageName;
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
- (void)setMyImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
[self setNeedsDisplay];
}
#end
Now my questions: why does this code get called, although I start the iPad simulator?
And more important it seems to corrupt the UIPopoverController because it looks like this:
http://awesome-apps.com/pic/ok.png
While it should look somehow like this:
http://awesome-apps.com/pic/nok.png
Besides it corrupts more in my App, but this should be it for starters :)
Can anyone help me with this? Have you ever had a similar experience?
So ignore the fact that it gets called when you run in the iPad simulator, because as you'll see in a minute even if you used two different categories (one for iPhone, one for iPad) you'd still have this problem.
Here's why:
You are using a category to override the UINavigationBar behaviour. I assume you know what that means - crucially any and all navigation bars in your app will use your supplied methods in the category.
This can cause problems if you're using standard apple elements that use UINavigationBars - the exact thing you're seeing in the popover controller. What's happening is the UIPopoverController uses a UINavigationBar. But because you've defined a category, the app assumes you want the popover navbar to use that category as well.
So that's why you're seeing your weird behaviour in your pop-over controller.
As long as you use categories you'll have this problem, because you can't selectively tell the system which bars should use your category.
I'd suggest you tell us exactly you're trying to customise in the navbar, because there are other ways to achieve customisation outside of categories.

UIButton with image losing its newly set image

I've got a UIButton (custom, with image) set up in IB which is an IBOutlet in my code.
In my viewDidLoad method in the viewController, I am trying to change the existing image of the UIButton
UIImage *newbuttonimage = [UIImage imageNamed:#"newbuttonimage.png"];
testbutton.imageView.image = newbuttonimage;
OK, that works when starting the app, but whenever you interact with the button (press it), it changes to the original image (set in IB). There's no other code to change images in the whole project, so what is going on?
Incidentally, when I put the above code in any other part of my project it doesn't change the image.
You should try using setImage:forState instead of assigning the image directly; there are multiple states of a UIButton and, if not set properly, may yield unwanted behaviors (akin to the ones you're seeing).
Doh! I should have used this code instead:
UIImage *newbuttonimage = [UIImage imageNamed:#"newbuttonimage.png"];
[testbutton setImage:newbuttonimage forState:UIControlStateNormal];