Passing UIImage to a function and Edit it externally - objective-c

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 alloc]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.

Related

How to refresh view in objective-c

I wanted to create a gallery. It loads different images based on the category, that a user selects. I used to populate images in UIImageViews.
My when selecting different categories is that it does not clear the previously selected images. This is my code for populating images.
-(void)refresh{
categoryLabel.text = treatment.treatmentName;
NSMutableArray *galleryImages =[NSMutableArray alloc] ;
galleryImages = [[PatientImage alloc]find:treatment.treatmentId];
int imgCount = [galleryImages count];
for(int i=0;i<imgCount;i++){
PatientImage *apatientImage = [galleryImages objectAtIndex:i];
UIImage *img1 = [UIImage imageNamed:apatientImage.imageBefore];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:img1];
myImageView.contentMode = UIViewContentModeTopRight;
myImageView.frame = CGRectMake(120+i*240,120.0,100.0, 100.0);
[self.view addSubview:myImageView];
UIImage *img2 = [UIImage imageNamed:apatientImage.imageAfter];
UIImageView *myImageView2 = [[UIImageView alloc] initWithImage:img2];
myImageView2.contentMode = UIViewContentModeTopRight;
myImageView2.frame = CGRectMake(120+i*240+300,120.0,100.0, 100.0);
[self.view addSubview:myImageView2];
}
}
First things first, You have some memory leaks there. You are allocating UIImageViews but are not releasing them anywhere, after you have added them to your view. I don't know if that applies to ARC, though. Same applies to your Mutable array, but I suppose you are releasing it after the 'for' loop somewhere, since it seems you posted code after omitting some of it.
As far as your actual question is concerned, I wouldn't do this this way. I would make the mutable array an object variable, and then fill it with my image views. When calling refresh again, I would first call -removeFromSuperview on each image view, then empty the array, then repopulate it and add the new subviews to my view. That is the simple way.
I don't know if you are using ARC, but you should be careful about memory management when using dynamically loaded views. Each time you add a view to another one, you increase its retain counter. You must then call release to remove ownership, and let the iOS runtime handle the rest.
Also note that operations such as this using views are expensive in terms of memory. So, another way of repopulating the gallery view is to just change the image an imageView holds. That will save you some memory, and time. In case the view doesn't have a constant number of images to be displayed, you can refine your algorithm to change the images on the already created image views, and then add more image views if necessary or delete the remaining ones, if any.
I hope I helped.
try at the start of refresh call
[[self subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
or
for (id imageView in self.subviews){
if([imageView isKindOfClass:[UIImageView class]]) [imageView removeFromSuperview];
}
call [tableview reloadData] if You are using tableview to show your gallery images
or call view's
[self.view setNeedsDisplay] method for refreshing the view.

Difference between UIImage and UIImageView

What is the difference between UIImage and UIImageView? Can someone explain it with an example?
Example:
UIImage *bgImage = [UIImage imageNamed:#"Default#2x.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:bgImage];
backgroundImageView.frame = [[UIScreen mainScreen] bounds];
UIImage Overview:
A UIImage object is a high-level way to display image data. You can
create images from files, from Quartz image objects, or from raw image
data you receive. The UIImage class also offers several options for
drawing images to the current graphics context using different blend
modes and opacity values.
Image objects are immutable, so you cannot change their properties
after creation. This means that you generally specify an image’s
properties at initialization time or rely on the image’s metadata to
provide the property value. In some cases, however, the UIImage class
provides convenience methods for obtaining a copy of the image that
uses custom values for a property.
Because image objects are immutable, they also do not provide direct
access to their underlying image data. However, you can get an NSData
object containing either a PNG or JPEG representation of the image
data using the UIImagePNGRepresentation and UIImageJPEGRepresentation
functions.
The system uses image objects to represent still pictures taken with
the camera on supported devices. To take a picture, use the
UIImagePickerController class. To save a picture to the Saved Photos
album, use the UIImageWriteToSavedPhotosAlbum function.
UIImageView Overview:
An UIImageView provides a view-based container for displaying
either a single image or for animating a series of images. For
animating the images, the UIImageView class provides controls to set
the duration and frequency of the animation. You can also start and
stop the animation freely.
New image view objects are configured to disregard user events by
default. If you want to handle events in a custom subclass of
UIImageView, you must explicitly change the value of the
userInteractionEnabled property to YES after initializing the object.
When a UIImageView object displays one of its images, the actual
behavior is based on the properties of the image and the view. If
either of the image’s leftCapWidth or topCapHeight properties are
non-zero, then the image is stretched according to the values in those
properties. Otherwise, the image is scaled, sized to fit, or
positioned in the image view according to the contentMode property of
the view. It is recommended (but not required) that you use images
that are all the same size. If the images are different sizes, each
will be adjusted to fit separately based on that mode.
All images associated with a UIImageView object should use the same
scale. If your application uses images with different scales, they may
render incorrectly.
UIImage contains the data for an image.
UIImageView is a custom view meant to display the UIImage.
In short:
You create an instance of UIImage object to hold image's data, like this:
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"/picture.jpg"]; //assuming your image is in your app's bundle
UIImage *img = [[UIImage alloc]initWithContentsOfFile:sourcePath];
You then create an instance of UIImageView either through IB or code to display your image on the screen, like this:
[imageView1 setImage:img]; //assume you already create an instance of UIImageView named imageView1
UIImage is a data object that holds image bytes.
UIImageView is a control that display UIImage data.
UIImage objects store data from an image (i.e. data from a png file)
UIImageView objects are used to display a UIImage
UIimage is an Object which stores data (Jpg, png...)
and UIImageView class which contains UIImage as a property.
UIImageView can have multiple UIImages, and UIImage is immutable.
That bothered me a long time ago, when you create an image you do:
var image: UIImageView!
But when you execute that "image" you do
image.image = UIImage.init(named: "image")
Its really easy, you use UIimageView, to create an image with UIImage.
UIImage displays the image, and UIImageView is a container for that UIImage.
UIImage holds the data as like (pictures) and displays it on UIImageView.
UIImageView is a type of container to display images.
UIImageView *myImage = [[UIImageView alloc]init];
[myImage setImage:[UIImage imageNamed:#"1.png"]];
create an instance for UIImageView as "myImage".This is helps for dispaly data.UIImage helps for hold the data of 1.png from the assets file in the xcode.

Overlay an UIImage above an UIImageView, possible? example code?

Is it possible to set an image over an other image,
respectively to overlay an image above an UIImage.
I give some code:
Headerfile:
#property (nonatomic,assign)UIImageView* imageView;
Implementationfile
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
The "imageView.image" has got an image and above that I will have this lamp, but later on it has to be possible to move it.
I searched some methods, but all methods gave me warnings.
Could you help me how to manage it? And is Core Animation an alternative to handle that?
Just add it as a subview. UIImageView is a UIView like any other.
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
[self.imageView addSubview:overlayImageView];
You should use another UIImageView. A UIImage doesn't refer to an actual view in the interface, it's used as a reference to an image to be reused in code.
UIImageView* lampImageView= [[UIImageView alloc] initWithImage:overlayImage];
This way you can move it around anywhere over the first image view.

Loading a UIImage in a UIImageView that is part of a UIViewController

So I have things nested like this in a nib:
UIViewController
|
|-UIView (container for some about text UILabel)
| |
| UILabel
|
UIScrollView (makes image scalable)
|
UIImageView
I'm trying to load the image of the UIImageView programmatically in another view controller, then pushing the new UIViewController onto a UINavigationController. Everything works fine, but when I assign the image, nothing appears on the new view.
So in a connectionDidFinishLoading: I do this:
UIImage* image = [UIImage imageWithData:imageData];
MyImageController* imageController = [[MyImageController alloc] init];
imageController.title = expandImage.title;
imageController.imageView.image = image;
[self.navigationController pushViewController:imageController animated:YES];
Nothing shows up besides what I designed in the nib. Any help?
Try these solution for your problem:
1.First of all, be sure that you get an image and you have an image data in the imageData variable.
2.most likely this solution is what you are looking for. declare a variable in the MyImageController of type UIImage, and set the propriety and synthesize for it, then in your connectionDidFinishLoading code, set the returned image to the variable that you declare, and in the MyImageController assign the variable to the imageController.imageView.
i hope that help you, and tell me what you got from these solution
good luck.

do I need to initialize the new view?

I admit I'm a newby to Objective-C but came pretty far, I guess. Now I'm having an issue and would need some help.
In my app I display images in portrait modes amongst other information and want to display them full screen in landscape mode. I learned that you'd do this defining a Landscape m, h and nib file and load this view when the device is moved to landscape orientation (working with an observer), right?
In portrait mode the images in the end depend on user interaction so the Landscape class needs to be told, which image to display. I created a putImage method in the Landscape class
-(void) putImage:(NSString *)theImage {
pict.image = [UIImage imageNamed: theImage];
NSLog(#"PICT %#",pict.image);
NSLog(#"IMAGE %#",theImage);
}
and call it from the portrait one and here comes the issue:
NSString *actualImage = [NSString stringWithFormat:#"Picture%i.jpg",selected];
pict.image = [UIImage imageNamed: actualImage];
[landscapeViewController putImage:actualImage];
that works perfectly every time except the very first one. So whatever I do I need to first make the App load a new image plus change to landscape orientation (being displayed no image) in any manner. Then after it always works displaying the correct images.
I added debug information and see that the correct image is assigned to pict in landscape class - however it seems it needs to be displayed before it really sets the image.
Any help you guys could give?
Not sure if this it it since your example code is pretty sparse. But when you load your first picture do you do that in init? If so, try to do it in viewWillAppear or viewDidAppear.
thank you for all the hints and tries to help. Now I found the solution myself ... here's what I did:
1) in the LandscapeViewController.h I defined an NSString:
#interface LandscapeViewController : UIViewController
{
IBOutlet UIImageView *pict;
NSString *actualImage;
}
2) I changed the putImage method to only assign the image name to my local NSSTRING variable:
-(void) putImage:(NSString *)theImage {
actualImage = theImage;
[actualImage retain];
}
3) loading the image I only do in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
pict.image = [UIImage imageNamed: actualImage];
[super viewWillAppear:animated];
}
So the answer to my question is almost answered - I guess that this issue arose because I did set the image before I loaded the view first time so that it was not instanciated. I was able to call the method but had no Image Object to set ... does that sound logical to you guys?
Thank you all again!