How to change the image inside an imageview? xCode 4.2 - xcode

I've been looking for hours now and i cant find a solution. i have an imageview, i have declare it as an IBOutlet, inside that imageview i have already put an image using the interface build of xCode 4.2.
Now my quesion, whats the line of code that it can change the image of the imageview?
The old ones like UIImage *image = [UIImageView imageNamed:#"image2.png"];
its not working because it says that *image is unused variable. it also gives me error at imageNamed it says No known class method for selector 'imageName'
Whats the new code in 4.2? or is it me doing something wrong?

myImageView.image = [UIImage imageNamed:#"image2.png"];
That should do it. UIImageView has an image property that you can set, either as above, or using the -setImage: method that the property provides. Also, from the docs:
Setting the image property does not change the size of a UIImageView.
Call sizeToFit to adjust the size of the view to match the image.
Your other questions:
its not working because it says that *image is unused variable.
In the code you provided, you're assigning the image to a local variable. If you don't then do something with that variable, there's no point in that code. Instead, assign the image to the image property of your image view, as described above.
it also gives me error at imageNamed it says No known class method for selector
'imageName'
That just looks like a typo in your code. The lack of a 'd' in 'imageName' is surely the problem.
Whats the new code in 4.2?
Nothing has changed with respect to setting the image of an image view, as far as I know. I think you've just got a couple little issues in your code.

It's you doing something wrong. You are mixing up UIImage and UIImageView classes. The former is for representing image data in memory. The latter one is representing UIImage instances in a view.
You should change your line to:
imageViewOutlet.image = [UIImage imageNamed:#"image2.png"];
Notice that I'm using UIImage class to assign the image to UIImageView's image property.

You are trying to declare a new variable and not using the ivar you declared with the IBOutlet

You need to assign
[UIImage imageNamed:#"image2.png"]
to the image property of the ivar of the IBOutlet.
For example if your IBOutlet is defined:
#property (nonatomic, strong) IBOutlet UIImageView *imageView;
assign like so:
imageView.image = [UIImage imageNamed:#"image2.png"];

This Works fine for me. oldImage is the IBOutlet of UIImageView. The setCellImage method is inside the Custom UITableViewCell Class.
- (void)setCellImage:(UIImage*)newImage{
[self.oldImage setImage:newImage];
}

Related

XCode images.assets asset catalog slicing been ignored and images still distored

I'm having trouble with using slicing in the Xcode image catalog. I have the image in an asset catalog, with the image named 'SquareReactionButtonCrisp' in that catalog. In my xib, I reference this name to set it as the background. And yet as I resize this button in the xib and rebuild, the image is stretched rather than it expanding from the slice lines. Any ideas?
Screenshots attached.
Thanks,
Sam
Asset catalog was introduced with Xcode 5 but to use the image slicing features of the asset catalog your deployment target must be set to iOS7. If your target is set to iOS 6 this may be the cause of your problem. See similar post where person answered their own question. How do you use an asset catalog image's slicing information programmatically?
My solution compatible with iOS 6/7 is using User Defined Runtime Attributes in Xib files. So that we don't have to write inelegant lines everywhere in the source code to replace the image set in xib with stretchable image with cap insets for the button.
Step 1: In Xib, select the button and set the User Defined Runtime Attributes in the Identity Inspector panel. You can define an attribute for setting the cap insets. For example, an attribute called "capEnabled" with just a simple boolean value to indicate we want to use default cap insets for the button. (I intended to attach screenshots but I was told I need at least 10 reputation to post image... :-( )
Step 2: Create a category on UIButton and add a property "capEnabled" and implement the getter and setter methods.
#interface UIButton (NBAHelper)
#property (nonatomic, assign) BOOL capEnabled;
#end
#implementation UIButton (NBAHelper)
-(BOOL)capEnabled{
UIImage *buttonBackgroundImage = [self backgroundImageForState:UIControlStateNormal];
CGFloat capLeft = buttonBackgroundImage ? buttonBackgroundImage.capInsets.left : 0;
return capLeft>0;
}
-(void)setCapEnabled:(BOOL)capEnabled{
if (capEnabled) {
UIImage *buttonBackgroundImage = [self backgroundImageForState:UIControlStateNormal];
if (buttonBackgroundImage) {
[self setBackgroundImage:[buttonBackgroundImage stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
}
}
}
#end
Step3: Import the header file of the category everywhere you want to use the new feature for the UIButton you created or simply import it into the .pch file.
Hope my solution is helpful to you.

setContentMode: for HJManagedImageV is not working for iOS

I am using a third party lib, HJCacheClasses, for loading images asynchronously. Here is the code for the same. It's pretty much straightforward:
NSMutableString *url = #"my url";
HJManagedImageV *asyncImageView = [[HJManagedImageV alloc] initWithFrame:frame];
[asyncImageView setBackgroundColor:[UIColor grayColor]];
[asyncImageView showLoadingWheel];
[asyncImageView setContentMode:UIViewContentModeScaleAspectFill];
[asyncImageView.imageView setContentMode:UIViewContentModeScaleAspectFill];
asyncImageView.url = [NSURL URLWithString:url];
[self.imageManager manage:asyncImageView];
[the_pScrollView addSubview:asyncImageView];
Everything works fine except that the image is centered and it not getting stretched/fitted according to the size of the view (which is of size of full screen). I know the image is small but I need to make it fit in the view to fill the view. but none of the setContentMode are working.
If you have a look at the source code of the HJManagedImageV class, you will notice the author hardcoded the imageView of the HJManagedImageV instance to always be aspect fit. So by default you will never get aspect fill content mode working.
There's an easy way to achieve this though, right after you set the url property of the managed image view, add also a callback to your own class, like so:
myImageView.callbackOnSetImage = (id)self;
And in the callback function set the desired contentMode for the imageView, just like this. This is tested and works for me. Good luck
-(void) managedImageSet:(HJManagedImageV*)mi
{
mi.imageView.contentMode = UIViewContentModeScaleAspectFill;
}
I found the answer myself which I feel is more simpler and strightforward. As Ican mentioned,in the source code of the HJManagedImageV class,the author hardcoded the imageView of the HJManagedImageV instance to always be aspect fit. So I just commented that code... For people who like to do the same follow the following steps
Go to HJManagedImageV.m file
search for a method named
-(void) setImage:(UIImage*)theImage
comment the line
imageView.contentMode = ...
or set the content mode you want to for the imageView. Note: This will set the content mode for all the imageViews using this class. So I prefer to go with first option.
In the code where you are using this class, set the content mode for the HJManagedImageV.

NSImageView fails to setImage?

I'm trying to create a simple ImageView that just loads an image from disk, but this isn't working (I put it in my applicationDidFinishLoading):
NSString *file = [#"~/update.png" stringByStandardizingPath];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
[wView setImage:image];
Apparently the image for the NSImageView isn't being set, because if I NSLog [wView image], I get (null). I'm pretty sure I'm making a beginner mistake here, but what?
The file and image load fine; if I draw the image to an NSView it shows up fine. wView isn't nil, and I tried making another project with the default window's view a custom NSImageView subclass whose initWithFrame: calls setImage. Still nothing.
(OP here with an actual account)
So it turns out that setImage doesn't work on an NSImageView that's explicitly constructed in IB, if you get what I mean. I made an NSView subclass that constructs an NSImageView inside it and calls setImage, and that worked fine. Really annoying.
Most likely, wView is nil. The most common reason for this is when a class that's instantiated in the nib with a connection to a view also gets instantiated a second time in code.

Help with Xcode drawRect using textfield to input value

i am currently trying to do a bar graph in XCode. I have tried CPGraph and all the stuff around but they are all out of date and i need help for XCode 4. I am completely newb with this and that's why i need your help.
Here is my code:
-(void)drawRect:(CGRect)rect
{
NSString *s = textField.text;
value = [s intValue];
NSLog(#"%i",value);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 60);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 400, value);
CGContextAddLineToPoint(context, 400, 100);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
Notice that i have changed one of the point value for "value".
What i have done is create a UITextField and i want to be able to write like 500 in the UITextField and that the bar adjust itself. Currently the NSLog tell me that value is equal to 0 even if, before building my app i manually enter a number in the TextField.
I have been searching fo 3 days now and everything i found give me errors and are incomplete as i says i know almost nothing about objective-c in xcode. I also noticed during my search that this type of line doesn't refresh real time if you dont tell him. I would like help with that too if that's part of my problem. If you want more information on my code just tell me.
I will be really grateful if somebody can help me.
here is my .h :
#interface draw2D : UIView
{
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end
OK, it looks like since you have your text field hooked up in IB to File's Owner, that is typically your app delegate which is not the class you have above as that is a UIView. So, not unless you changed File's Owner to this UIView then your text field is not hooked up. So, typically what I do is to add a UIView to the xib window in IB (if you are not using the one that comes free when you create a XIB). Then that is what I'll attach the text box to, not File's Owner. Kind of begs the question of how anything is attached to Files Owner not unless you have a UITextField declared in there too (again providing you didn't change the class from the app delegate to this view class).
I know that if someone is new to this is is almost like greek. I developed in Java, C/C++, C#, etc for years and this was like learning how to program all over again.
If you are new to this I highly suggest the iPhone dev book by Big Nerd Ranch. I found it very useful.
OK, I found out how to redraw my line with a button refresh.
What I did was create a button and link it with my UIView where my line is being drawn. Inside I wrote [self setNeedsDisplay]; and each time I click the button it refreshes the view and gives me back my NSlog that I set in the same method as my drawRect.
Many thanks again Merky.

Apple Interface Builder: adding subview to UIImageView

I created UIImageView with the help of Interface Bulder. Now I want to place label inside it (as its subview). In code I can type something like: [myUIImageView addSubview:myUILabel]; But can I do it with the help of IB? I found the solution for UIView, but can't find something similar for UIImageView.
You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome.
So there is an easy workaround. Instead of dragging an instance of UIImageView in the nib, just drag a UIView and change its class to UIImageView from UIView (cmd+4 option of inspector). The only difference you find in the nib for default imageView instance and your new UIImageView subclass instance is: you cannot set image to your new imageView from nib (cmd+1 option). So, in the -viewDidLoad method of its appropriate viewController, set image to this outlet of UIImageView.
By doing so, you are free to add subviews to your "now UIImageView" instances in interface builder, which is much easy.
I would like to add answer.
While it sucks you cannot add subview to UIImageView, you can get the same effect by incorporating UIView with transparent (clear color) background.
Then put the UIImageview BEFORE it.
So the UIView has the subviews and the UIImageview is the background of the UIView.
I think that's apple's intent.
Here is a screenshot:
Here is the result:
Don't forget to set background as clear color
Now if someone could actually point me to a tutorial how to do this it'll be great. I spent hours doing it the checked answered way. The checked answer is a fine answer but very unintuitive because you can't see your background image clearly while working. I think mine is the proper way to do so.
In latest XCode(4.5) there is an option to drag and drop the required controls to the parent.
It is quite easy.
Attached screen shot for the same. I dragged the Label/TextField and Button to UIImageView
Use this code:
UIImage *image = [UIImage imageNamed:#"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view insertSubview:imageView atIndex:0];
(replace background.png with image) (replace atIndex:0 with whatever place in the .index root you want to insert the image and your off.

Resources