Loading an image sequence in InterfaceBuilder -ObjectiveC - objective-c

Using Actionscript and or in the Flash IDE you can either instantiate bitmaps from the library, or simply import bitmaps into the timeline of a MovieClip to create an image sequence.
How would you do the same thing in InterfaceBuilder and-or using ObjectiveC? Do I need to create a new view for each and every image?

You could store them in an array.
Something like so:
NSMutableArray *_imageArray = [NSMutableArray array]; //or you can alloc-init if needed
UIImage *_firstImage = [UIImage imageNamed:#"image1.png"];
UIImage *_secondImage = [UIImage imageNamed:#"rambo.png"];
etc...
[_imageArray addObject:_firstImage];
[_imageArray addObject:_secondImage];
And then go through them in a NSTimer loop or something similar.
Hope it helps

Related

Really simple xcode array issue

I'm programming a quiz app in xcode and I have 10 images which I want to use as UIImage views. I want to know how to create an array of all those images.
I also have a switch statement where I want to call 1 image from that array to be the UIImage. If you could explain how to call an image from an array as well I would be very grateful :)
Thanks in advance !!
Instead of storing UIImages in the array you can also store image names in array
NSArray *imagesArray = #[#"<image-name-1>",#"<image-name-2>",...,<image-name-10>];
when you want to get the image to set to UIImageView
UIImageView *imageView = [[UIImageView alloc] initwithFrame:<your-frame>];
[imageView setImage:[UIImage imageNamed:imagesArray[<your-index>]];
Let me know if you need any more info.
If you need to call an object from an array, objectAtIndex: can do the job for you. It doesn't matter whether you store UIImage instances of UIImageView instances, this will work with any form of object.
Here's a little example that will call each item from the array using objectAtIndex::
NSArray *_imageViews = ...
for (int idx; idx < _imageViews.count; idx++)
{
UIImageView *_imageView = [_imageViews objectAtIndex:idx];
/* do whatever you have to do with the image view */
}

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.

How can I allocate UIImageView dynamically in iphone sdk?

I want to add number of images from library to a view. The number we want to add to the view is not known. How can I allocate UIImageViews dynamically. And I also want to drag those imageviews on the UIView. Please help me.
Thanks in advance.
Your 'question' consists of way too many parts (the dragging is a different question entirely, one I advice you to look up on Google). And the adding part is easy. The hard part is; where are you getting those images from? Assuming you have an array with image names, it would be something like this;
for( NSString *imageName in (NSArray *)imageNames )
{
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// .. you might want to set a frame to your imageView .. //
[self.view addSubview:imageView];
[imageView release];
}
Of course, this is not what you want, but with the little information provided, it was the best I could do with my spare time.

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