Displaying UIImage - iphone

I'm trying to displaying an image from file on my ViewController in Xcode. I have in my ViewController.h:
UIImageView *image;
#property(nonatomic,retain) IBOutlet UIImage* image ;
ViewController.m
- (id)initWithImage:(UIImage *)image
{
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nameoffile.jpg"]];
self.image = image;
[image release];
}
But no luck. It won't let me link my UIImage instance in the xib.

Try to use this one.You know you can't get IBOutlet on UIImage. So you need to write UIImageView instead of UIImage.
#property(nonatomic,retain) IBOutlet UIImageView* image ;

As the UIImageView is created when the NIB file is loaded, there is no need to make it an instance variable, so remove it from your header file:
UIImageView *image;
Next ensure that your UIImageView outlet is actually connected correctly to the image view within the NIB file. You will see a filled circle when it is connected (this is from a Beginning iOS Development example, which uses ARC, so ignore the weak keyword):
And finally you should be able to set the image view's image in the viewDidLoad method (there is no need to use an instance variable for the UIImage either, as it will be retained within the UIImageView):
- (void)viewDidLoad
{
[super viewDidLoad];
imageView.image = [UIImage imageNamed:#"nameoffile.jpg"];
}

Check the Error of your code.
#property(nonatomic,retain) UIImageView *imageView;
#property(nonatomic,retain)UIImage* image ;
- (id)initWithImage:(UIImage *)image
{
_imageView = [[UIImageView alloc] initWithImage:image];
_imageView.frame=self.view.frame;
[self.view addSubView:_imageView];
[image release];
}

Related

UIImageView alloc] initWithImage using IBOutlet not working

I've typically followed the pattern of creating an object in my method (like viewDidLoad), have the property hold onto it, then release the original object created. So something like this:
NSArray *myArray = [NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
self.array = myArray;
[myArray release];
I thought I could do the same thing with UIImageView. I have a UIImageView created in my .xib. I have an outlet to it that is a property like so
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
In my viewDidLoad, I do:
UIImageView *imgView = [[UIImageView alloc] initWithImage;[UIImage imageNamed:#"arrow.png"]];
self.imageView = imgView;
[imgView release];
If I run this, I do not get anything on screen. However, if I do just this in my viewDidLoad instead:
[self.imageView setImage:[UIImage imageNamed:#"arrow.png"]];
I do get my arrow image on screen. Is there something different about UIImageView that I am missing here?
Edit because of first response: Is UIImageView different than UITableView in that I need to add it as a subview to my current view? When I create a UITableView in IB, I do not add it as a subview in viewDidLoad.
Thanks.
You are missing the point behind how IBOutlet work. If something is specified in .xib then it is already allocated for you :) You just use it.
Hence [self.imageView setImage:[UIImage imageNamed:#"arrow.png"]]; works since you just assigned an image to it.
However, when doing
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
self.imageView = imgView;
[imgView release];
you actually deallocated the imageView instance created by IB at the self.imageView = imgView; line and set a new one and neither did you added it as subview to anything. Since it is a new instance it will need to be added !! But anyways this approach is wrong if you are using IBOutlet
You need to add self.imageView to your view hierarchy.
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView = ...
// You are missing this part!
[self.view addSubview:self.imageView];
...
}
PS. You dont need to mark your UIImageView property as IBOutlet since you are not using Interface Builder to build your view.

Adding a UIImage to UIImageView programmatically

In .h file I declare this;
IBOutlet UIImageView *image;
In .m file;
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
image = [[UIImageView alloc] initWithImage:image1];
image.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
and I connected the UIImageView from the Interface builder. But I need to do this Only by code (without using the Interface Builder). Can someone help me modify the code so that I could do this only by code?
I THINK you have some problem in displaying a remote image in uiimageview so i thing u should do that fashion.
NSData *receivedData = [[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://image.com/image.jpg"]] retain];
UIImage *image = [[UIImage alloc] initWithData:receivedData] ;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0,0, 50,50);
[self.view addSubView:image];
[image release];
[imageView release];
and i connected the UIImageView from the Interface builder
That was a mistake. If you do that, the image view pointed to by your image instance variable will be the wrong one - the one in the nib. You want it to be the one that you created in code.
So, make no connection from Interface Builder; in fact, delete the image view from Interface Builder so you don't confuse yourself. Make sure your instance variable is also a property:
#property (nonatomic, retain) UIImageView* image;
Synthesize the property:
#synthesize image;
Now your code will work:
UIImage *image1 = [UIImage imageName:#"http://image.com/image.jpg"];
self.image = [[UIImageView alloc] initWithImage:image1];
// no memory management needed if you're using ARC
[self.view addSubview:self.image];
You will need to play with the frame until the location is correct. Note that the frame will automatically be the same size as the image, by default.
you dont need to connect. This code will work without connecting. Leave IBOutlet out.
UIImage *someImage = [UIImage imageName:#"http://image.com/image.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:someImage];
[self.view addSubView:imageView];

UIscrollview initial image size

I am new to objective-c I am building an app that has a scrollable image but can't get the initial image size to fit the start view at 320 x 411 when the tab is selected, as this is for predominantly medical images the user will need to have an overview and the ability to zoom in on a detail. So the intention is to have the initial image at 30% zoom rather than 100%.
I have pasted the code below any help would be appreciated
I have attached a screen shot showing the problem where only part of the flowchart is shown when the tab is selected.I want the whole flowchart to be shown when the tab is selected.
From AlgorithmViewController.h file
#interface AlgorithmViewController : UIViewController {
UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
From AlgorithmViewController.m file
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"photo1.jpg"];
imageView = [[ UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[(UIScrollView *)self.view setContentSize:[image size]];
[(UIScrollView *)self.view setMaximumZoomScale:2.0];
[(UIScrollView *)self.view setMinimumZoomScale:0.4];
}
add scaleToSize to your UIImage
UIImage *image = [[UIImage imageNamed: #"photo1.jpg"] scaleToSize:CGSizeMake(320, 411)];
and
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 411)];
program comes up with an error "UIImage may not respond to scaleToSize".
Solved excellent tutorial on the subject at
http://cocoadevblog.com/iphone-tutorial-uiimage-with-zooming-tapping-rotation

How to call a method from a different class?

So, I am trying to make a shared method in my Brain class which will check the label on a pressed button (buttons of items are in different classes) and then accordingly add a image to a ShoppingList view minding the order of adding (first image added goes to position 0,0, second to position 80,0 etc.).
I have Breakfast class which represents breakfast ingredients and I wanna add a photo of a Tea to another view called ShoppingList.
I wrote a method in Brain that adds an image to a imageView and returns an imageView which is then locally passed to the button pressed.
It builds but when I press the button Tea, application crashes.
Here is my code:
Brain.h
#interface Brain : NSObject {
#private
UIImage *image;
UIImageView *imageView;
}
#property (retain) UIImageView *imageView;
#property (retain) UIImage *image;
- (id)performOperation:(NSString *)operation;
#end
Brain.m
#implementation Brain
#synthesize imageView;
#synthesize image;
- (UIImageView *)performOperation:(NSString *)operation
{
if ([operation isEqual:#"Tea"]) {
image = [UIImage imageNamed:#"Tea_photo.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(0, 0, 80, 80);
return imageView;
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
else return 0;
}
#end
Breakfast.h
#interface Breakfast : UIViewController {
IBOutlet ShoppingList *shoppingList;
Brain *brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender;
- (IBAction)goToShoppingList;
- (IBAction)goBack;
#end
Breakfast.m
#implementation Breakfast
- (Brain *)brain
{
if (!brain) brain = [[Brain alloc] init];
return brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender
{
NSString *operation = [[sender titleLabel] text];
UIImageView *imageView = [[self brain] performOperation:operation];
[shoppingList.view addSubview:self.brain.imageView];
//UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//imageView.frame = CGRectMake(0, 0, 80, 80);
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
- (IBAction)goToShoppingList
{
[self presentModalViewController:shoppingList animated:NO];
}
- (IBAction)goBack
{
[self dismissModalViewControllerAnimated:NO];
}
#end
PLEASE HELP, its for my thesis.
IBOutlet ShoppingList *shoppingList;
Why make this IBOutlet? Isn't ShoppingList a class that you created. Not that this solves your crash. For that u need to post the crash log....
And also in you code I can't see any allocation for shoppingList so how can you be able to use it. You need to allocate the object in the class Brain otherwise it doesn't make any sense.
I have not gone through your code. But on the basis of your description I can suggest you to create a protocol

UIImageView not showing Image

I have the following code:
#interface NeighborProfileViewController : UIViewController {
UIImageView * profPic;
UITextView * text;
UIButton * buzz;
NSString * uid;
NSMutableData *responseData;
NSDictionary * results;
}
#property (nonatomic, retain) IBOutlet UIImageView * profPic;
#property (nonatomic, retain) IBOutlet UITextView * text;
#property (nonatomic, retain) IBOutlet UIButton * buzz;
#property (nonatomic, retain) NSString * uid;
#end
Here's what I have in the viewDidLoad on the .m file
#implementation NeighborProfileViewController
#synthesize uid;
#synthesize profPic;
#synthesize buzz;
#synthesize text;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = //some URL to the picture;
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
self.profPic = [[UIImageView alloc] initWithImage: img];
}
I think I wired up the UIImageView via IB correctly. I have an outlet from the UIImageView to the profPic in the Files Owner. What am I doing wrong?
If you are setting a default image is that staying visible after you call self.profPic = [UIImageView] or is it being removed from the screen?
I think this problem comes when self.profPic releases the old image view to replace it with the one you've just created. The old UIImageView instance, along with all the properties which you defined in IB, are probably automatically removed from the superview. Which is why you're not seeing the downloaded image.
If you used IB to create the UIImageView then you don't want to create a new ImageView and assign it to profPic (which is already a fully instantiated UIImageView). Try calling [profPic setImage:img]; which will just change the image in the profPic imageview.
#Equinox use
[profPic setImage:img];
instead of
self.profPic = [[UIImageView alloc] initWithImage: img];
although using autorelease is not an issue here.
you can also do something like this
UIImage *img = [[UIImage alloc] initWithData:data] ;
[profPic setImage:img];
[img release];
the problem with
self.profPic = [[UIImageView alloc] initWithImage: img];
is that you are now creating another memory location for profPic and that means profPic will no longer point to the UIImageView in your IB any more