Passing UIImage to a function and Edit it externally
Let's say we have this part of code for a UIView subclass:
self.myImage = [UIImage imageNamed:@"img1.jpg"];
[self myFunc:self.myImage];
myFunc is here defined:
-(void)myFunc(UIImage*)img{
UIImageView *imgView = [[UIImageView allo开发者_如何学JAVAc]initWithImage:img];
[self addSubview:imgView];
}
Now i show in my view img1.jpg, if i decide to change self.myImage AFTER myFunc call
self.myImage = [UIImage imageNamed:@"img2.jpg"];
why i continue to show img1 and not img2 ??? myFunc receive a UIImage pointer not directly an object... i'm pretty confused and i missed something really important i think.
Edit --
I'd like to have many UIImageView, and every image ivar of these view must pointing to the same image address, changing image at this address every UIImageView have a new image how can i do that ?
Given your code. Let say that when you load img1.jpg into memory it went to address 1000. And you use the ivar myImage to point to it. When you pass that ivar to your function and create the UIImageView it also points to img1.jpg.
Now, when you load img2.jpg into memory it went to a different address, for example 2000. And then updated the ivar myImage to point to that new location. img1.jpg is still in address 1000 and is still being used by the UIImageView.
If you want to change the image of the UIImageView you will have to use the image property of that class.
The UIImage that you've given to the UIImageView does in fact not know itself the view it belongs to. It may be used in different views. You have to ask the UIImageView object for the current image that it displays. If you want to change the displayed image you would write:
self.imgView.image = [UIImage imageNamed:@"img2.jpg"];
Better you add a property for the UIImageView so that you can access it from everywhere in the class.
精彩评论