assign image to image view

21 views
Skip to first unread message

cabhara

unread,
Nov 22, 2009, 10:07:07 PM11/22/09
to iPhone SDK Development
Hi,

I'm trying to display an image dynamically and it only works if I make
a sub view. Is that necessary?
Why does it not work to assign the image only?
Does not work:
UIImage *labelImage = [UIImage imageNamed:@"test.png"];
[self.myImageView initWithImage:labelImage];

works:
UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"test.png"]];
[self.myImageView addSubview:tmp];
[tmp release];

Thanks
Christina

Vaish

unread,
Nov 24, 2009, 6:26:50 AM11/24/09
to iPhone SDK Development
hi...

i ran into an almost similiar issue some time ago.Try this code:

UIImage *labelImage = [UIImage imageWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"test" ofType:@"png"]

Do post back if you are stuck again.

Thanks

David Phillip Oster

unread,
Nov 24, 2009, 4:54:03 PM11/24/09
to iPhone SDK Development
The line: [self.myImageView initWithImage:labelImage]; can never
possibly be correct.

If you expand it out into traditional syntax, it would be:
[self setImageView:[[self imageView] initWithImage:labelImage]];

1.) One only sends init to a freshly alloc'ed object, so the inner
part should be:
[[[UIImageView alloc] initWithImage:labelImage] autorelease]

2.) assigning to your imageView field doesn't change the view
hierarchy. You'd need to remove the old imageView and insert the new
one in the same place.

3.) You probably meant:
NSAssert(self.myImageView, @"");
self.myImageView.image = labelImage;

which just changes the image retained by the imageView, but it assumes
that the imageView already exists.
The NSAssert will dumo you into the debugger if myImageView is nil.

-- David Phillip Oster




cabhara

unread,
Nov 26, 2009, 12:16:55 AM11/26/09
to iPhone SDK Development
what I'm doing right now is the following, and it is working ok so
far.... I will give the direct assignment a try.

//remove existing sub views
NSArray *myviews = [self.myImageView subviews];
if ([myviews count]>0) {
[[myviews objectAtIndex:0] removeFromSuperview];
}

NSString *imageName=.... //determines image name

//assign image to temporary view
UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:imageName]];

//add temp view as sub view
[self.myImageView addSubview:tmp];
[tmp release];




On Nov 24, 3:54 pm, David Phillip Oster <davidphillipos...@gmail.com>
wrote:

cabhara

unread,
Nov 26, 2009, 12:26:35 AM11/26/09
to iPhone SDK Development
hmm. looks like this is working and much easier than what I did.

NSString *imageName=... //get image name

self.myImageView.image = [UIImage imageNamed:imageName];


but somehow the pictures are less clear than when using the sub view.

probably has to do with the the sizing and other settings....

Christina


On Nov 24, 3:54 pm, David Phillip Oster <davidphillipos...@gmail.com>
wrote:

iDeveloper

unread,
Nov 29, 2009, 12:39:19 AM11/29/09
to iphonesdkd...@googlegroups.com
Avoid using imageNamed: as far as possible. That is one of the worst
methods available in terms of memory management.
If you want to us one of the convenience methods, try using
imageWithContentsOfFile:

UIImage *image = [UIImage imageWithContentsOfFile:[[[NSBundle
mainBundle] resourcePath]
stringByAppendingPathComponent:@"YourImage.jpg"]];
[myImageView setImage:image];
> --
>
> You received this message because you are subscribed to the Google
> Groups "iPhone SDK Development" group.
> To post to this group, send email to iphonesdkd...@googlegroups.com
> .
> To unsubscribe from this group, send email to iphonesdkdevelop...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/iphonesdkdevelopment?hl=en
> .
>
>

Reply all
Reply to author
Forward
0 new messages