Unable to access UIImageView image - iphone

Currently attempting to access a UIImage property of UIImageView to instantiate another UIImage object. Simply I'm trying to do this.
UIImage *myImage = myExistingView.image;
The property will not set however. I keep getting nil values for myImage.
I would like to copy the image, eventually doing something like
UIImageView *newImageView = [[UIImageView alloc] initWithImage:myImage];
but I can't seem to get it down. Suggestions?
Edit. Most recent attempt that has not worked.
UIImageView *newView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithData:UIImagePNGRepresentation(myImageView.image)]];

You have to Initialize your UIImage in this way UIImage *myImage = [UIImage imageNamed: #"imgCheckSelected.png"];
After you will pass myImage to newImageView. I`m not sure that you can save a UiImage inside another UIImage.

Do this for accessing images
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imageName"]];
It will dispaly image.

I did this and it works..
UIImageView *someNewImageView = [[UIImageView alloc] initWithImage:yourExistingView.image];
someNewImageView.frame = CGRectMake(50, 50, 150, 150);
[self.view addSubview:someNewImageView];
[someNewImageView release];
Notice that yourExistingView is an UIImageView propertie added in Interface builder.
Have you checked the frame of your new UIImageView??
did you set the image in yourExistingView??
did you add the new imageview to view??

Related

Display image in a UIImageView in iPhone

I do not wants use a picker view to pick a image. I have image name abc.JPG which is in iPhone image library, can I programmatically fetch this image and put on UIImageView?
If the image isn't in your application resource and you don't want to use UIImagePickerController then you can use ALAssetsLibrary to fetch the images on user albums or camera roll.
Take a look here:
http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:#"abc.JPG"];
[self.view addSubview:myImage];
or if you have already uiimageview with name myImage then just write:
myImage.image = [UIImage imageNamed:#"abc.JPG"];
Maybe:
UIImageView *myImageView = [UIImageView alloc] init];
myImageView.image = [UIImage imageNamed:#"abc.JPG"];
Try to follow this tutorial.
Even if you don't want to, I think you have to use UIImagePickerViewController to pick the image, otherwise the image will be picked from Resources in your project.

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];

how to display images in imageview in iphone

Iam new to iphone currently my problem is
i displayed 6 images in 6 image views It was not showing images in imageview I think the imageviews are in large size
i implement code like this.
imageview1 = [[UIImageView alloc] init];
UIImage *img1 = [UIImage imageNamed:#"wolf.jpg"];
[imageview2 setImage:img1];
so plz send me correct code for this imageview.
Thank u in advance.
bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,480,300)];
bgImage.image = [UIImage imageNamed:#"Default.png"];
[self.view addSubview:bgImage];
Also check have you add image file to your project, by Add - > Existing File
First of all, you're adding the image 1 to imageview 2 - are you sure you want to do this?
Secondly, you need to add the imagview to the view hierarchy, for example by adding it to a ViewControllers view, like this: [[self view] addSubview:imageview1] (If this code is situated within the View Controller class).
imageview1 = [[UIImageView alloc] init];
imageview1.image = [UIImage imageNamed:#"wolf.jpg"];

Is there a way to add images of type .jpeg , .gif and tiff... etc in iphone sdk?

I got one problem that how can i display the .jpeg and other type of files in iphone sdk. Is there any solution to this. please help me.
Thank you,
Madan Mohan.
Create a UIImage like this:
UIImage *myImage = [UIImage imageNamed:#"image.jpeg"];
Display it in a UIImageView:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
And add the UIImageView as subview to your existing view:
[view adsdSubview:myImageView];
you can use the following code to display image from the URL
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:#"yourURL"]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 250, 250)];
imgView.image=[UIImage imageWithData:imageData];
[self.view addSubview:imgView];

Why won't my UIImageView appear as a subview?

UIImage *image = [[UIImage alloc] initWithContentsOfFile:#"popup.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[image release];
[imageView release];
The code sits within a UIViewcontroller object. It compiles fine but, when run, the subview does not appear to be added.
I'm tired.
initWithContentsOfFile requires the complete file path, not just the file name.
Replace the line where you initialize UIImage with the following:
NSBundle *bundle = [NSBundle mainBundle];
UIImage *image = [[UIImage alloc] initWithContentsOfFile: [bundle pathForResource:#"popup" ofType:#"png"]];
A simpler way will be to use the imageNamed: method of the UIImage class
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popup.png"]];
And you dont have to worry about the path-to-file details. Just that the initWithImage: method caches the image. Depending upon your application might be good or bad idea.
Hope this helps!
Try to use:
UIImage *image = [UIImage imageNamed:#"popup.png"];
and, off course, get rid of the [image release]; because in this solution the image is autoreleased.
Notice that this solution won't be good if you have more than one image with this name (in different folders/groups)...
your imageView needs a frame.
imageView.frame = CGRectMake(0,0,320,480);
I know this question is old now, but some users can still benefit from an answer.
Like others have said, try using [UIImage imagedNamed:] instead of initWithContentsOfFile.
And the reason it is not showing up on screen is you did not set a location for the image. You can fix this by either setting a frame for the image view or setting its center at a single point. To place the image view in the center of the screen:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = [self.view center];
// or if you want to set a frame instead
// CGRect imageFrame = CGRectMake(x,y,width,height);
// [imageView setFrame:imageFrame];
[self.view addSubView:imageView];
Enjoy :)