How can i set images after generating imageview in ios? - ios

Suppose I generated the 100 image views. First of all I wish to generate all 100 image views and add them in my scroll view.
So image view are clearly display it’s in position using background color
Then after I want to set images into the image view. I have to types of Dictionary (Which comes from the another class). One Dictionary into set Already Downloaded images and another one into only generate object (Remaining download images). I am downloading this image Asynchronously.
Then how can I set images into the image view?
-(void)GenerateImageView{
for (int ivalue = 0; ivalue < 100; ivalue++) {
imageView = [[UIImageView alloc] init];
//Give tag to the image view
imageView.tag = ivalue;
[imageView setBackgroundColor:[UIColor whiteColor]];
CGFloat xOrigin =ivalue *ascrollView.frame.size.width+50;
imageView.frame=CGRectMake(xOrigin40,imgYpos,ascrollView.frame.size.width-20, scrollViewHight);
//set imageview into the scroll view
[ascrollView addSubview:imageView];
}
}

Then how can I set images into the imageview?
UIImageView *imageView = [ascrollView viewWithTag:some_tag];
if ([imageView isKindOfClass:[UIImageView Class]])
imageView.image = your_UIImage_object;

u can not access the directly image view.And also make sure ur tag not start with 0.bcs 0 is a by default main view tag
imageView = (UIImageView *)[ascrollView viewWithTag:some_tag];
imageView.image = your_UIImage_object;

You could use the code Avt suggested, but instead of reinventing the wheel, I would suggest to look into
AFNetworking
an open-source networking lib. It comes with an extension to the UIImageView which allows you to load the images asynchronously out-of-the-box. There are many other useful features too.
After importing the extension you could then use:
[imageView setImageWithURL:[NSURL URLWithString:#"yourImageURL"]];
the image gets loaded asynchronously and automaticly set when it's done loading.

Related

Should I use GIF file or animation code on iOS?

I intend to make a splash screen on iOS consists of a person, a chair objects. Each of these objects has seperated aspects like the hands, head, body and feets that animated together. I wonder which is better way to go? Importing a GIF file or coding CALayer objects then adding animation?
You can't use GIF files or any code for animation for your launchscreen (splash screen), you can only use a static image, a PNG or JPG (if the launchscreen is a storyboard).
So, if you want your app's startup with some animation then you should manage it in your first view controller.
You can animate imageview with image sets like,
UIImageView* myImageViewForAnimation = [[UIImageView alloc] initWithFrame:self.view.bounds];
myImageViewForAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
myImageViewForAnimation.animationDuration = 1.0f;
myImageViewForAnimation.animationRepeatCount = 0;
[myImageViewForAnimation startAnimating];
[self.view addSubview: myImageViewForAnimation];
Update (as asked in comment) :
You can get your view on which you have added gesture recognizer in your action method or you can set tag to every imageview and handle it in action method.
For example,
-(void)tapOnProfileImage : (UITapGestureRecognizer*)recog{
UIImageView *tempView = (UIImageView *)recog.view;
// or
if (recog.view.tag == 1) {
// image 1
}
if (recog.view.tag == 2) {
//image 2
}
}
So, on every image view, you should add target with selector - tapOnProfileImage and you can differentiate it as mentioned in above code snippet!
You can use gif image but not directly on launch xib. You create one view controller and load gif image on that view controller, once animation is completed then remove this controller from navigation stack. https://github.com/Flipboard/FLAnimatedImage use this gif image view control.

how to load two images with Lazyloading in one cell

In my application I have used Lazyloading for displaying image from url. But now i want to display two multiple images in one cell. How can i do this please help me.
Maybe this will help you. add this code in cellfor row and for Download Image use SDWebImage
UIImageView *imgView1=[[UIImageView alloc]init];
imgView1.frame=CGRectMake(0,0,20,20);//Your Frame
imgView1.image = [UIImage ImageNamed:#"a"]; // your image or download image code or use Sdwebimage
[cell.contentView addSubview:imgView1];
UIImageView *imgView2=[[UIImageView alloc]init];
imgView2.frame=CGRectMake(25,0,25,25);//Your Frame
imgView2.image = [UIImage ImageNamed:#"b"]; // your image or download image code or use Sdwebimage
[cell.contentView addSubview:imgView2];
If you want to add more images then add same code and just give frame.

Instantiate an image to appear at several different points on the screen?

I have an image called Empty.png, it is a small-ish square tile, how could I instantiate the image to appear at several different points on the screen?
Thank you for any help in advance :)
You can can place UIImageView's wherever you want the image to appear.And then set the image property of each image view as this image. (UIImage object).
If you are using interface builder then you just have to type in the name of the file in the attributes inspector of the imageview in the interface builder.
Or you could do this:
UIImage *img = [UIImage imageName:#"Empty.png"];
imageView.image = img; //Assuming this is your utlet to the image view.
It depends on how you want to use it.
If you just draw it with core graphics, let's say in drawInRect: or so, then you simply draw it several times.
If you want to display it within one or a number of image views, then instanciate your UIImageViews and assign the same object to all of them. Or let the Interface Builder do the instanciation for you. But you cannot add a single UIView object several times to one or a number of subview-hierarchies. If you add a UIView as subview to a view then it will disappear from the position where it was before.
10 UIImageView may use the same UIView but you need 10 UIImageViews to display all of them.
The same applies to UIButtons and every UI-thing that has an image or background image.
this will get you one image into some view
CGPoint position = CGPointMake(x,y);
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"Empty.png"];
img.frame = CGRectMake(position.x, position.y, img.image.size.width, img.image.size.height);
[someUIView addSubview:img];
if you make an array for the positions (x,y) of all the images, then you can just run it in a for loop and it will place the images into the view at the positions you want
note: CGPoints cant be stored in an NSArray since its not an NSObject type, either use a C/C++ array or use something else that can fit into a NSArray

How to set the imageView property in a UITableCellView to show only a background color?

I have a custom class inheriting from UITableViewCell class that shows either an image (left to the title) or a generic dark-colored square if the image is not available). The following code shows a dark square on a light-colored cell background:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(11, 6, 40, 40)];
[imageView setBackgroundColor:kBackgroundGreyColour];
[cell.contentView addSubview:imageView];
However, instead of creating a custom subview in each table cell I would rather like to use the existing imageView property of the generic UITableViewCell class and modify it somehow to show the square as the code above does. This is what I am trying at this moment:
UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(11, 6, 40, 40)];
[iv setBackgroundColor:[UIColor redColor]];
self.imageView.hidden = NO;
self.imageView.opaque = iv.opaque;
self.imageView.alpha = iv.alpha;
self.imageView.image = iv.image;
[self bringSubviewToFront:self.imageView];
[self.imageView setBackgroundColor:[UIColor redColor]];
I added all those lines to set as many of the existing UIImageView properties to the same values as the created UIImageView instance in the first code snippet, and yet the second code snippet doesn't show any dark square. It just doesn't show anything at all and the cell looks like there is just the light background and no image view visible. But I see that the imageView property is not nil so executing all those lines of code in the second snippet should show something?
However, as soon as I assign a new image to the imageView property (e.g. self.imageView.image = [[UIImage alloc] init...], the square shows the assigned image without problems.
Edit: Just a note that in the second case I am setting the frame of the imageView in layoutSubview function, e.g.:
-(void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(11, 6, 40, 40);
}
So my questions are:
1. Which properties of the existing imageView property I would need to set and to what values so that the code will show a square filled with a specific color (like the first snippet of code does)?
Is there a way of creating the UIImage programatically so that it shows only a background color without any image associated with it (and which I could use to set the imageView.image property to show that color).
Is it possible to replace the existing imageView property in a UITableViewCell class with a custom view without adding a custom subview (like the first code snippet did), so that I can show a placeholder UIView with a background color when the image is not available?
The reason why your code doesn't work, is as you guessed; Because when you set the background colour of an imageview, it doesn't create anything on the image property.
And, you've figured out that you can't directly set the imageview property of the cell either.
I'd say your best bet, is the former option; To create a UIImage programmatically.
Although, I'd highly suggest simply creating one in your favourite image editing software then including it in the bundle. It makes for easy replacement later, for when you may get a better image, and next to no code and effort required to replace.
But if you still wish to do it all programmatically, it's not as simple as you'd hope.
CGRect rect = CGRectMake(11, 6, 40, 40);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [kBackgroundGreyColour CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageview.image = image;
Should do the trick.
This defines the image size, creates a graphics context (think of it as a canvas), picks your grey colour to use, paints the canvas with it, then scans it into your computer into the small little size you wanted.
The little green imp does it all behind the screen (Sorry, too much Terry Pratchett).

Center [UIColor colorWithPatternImage]?

I couldn't find anything on how you can center the 'image' when you use (maybe you can't):
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background"]]];
Setting my [self setContentMode:UIContentModeCenter]; doesnt help.
Do I have to manually draw the image (either in drawRect or set the content of the CALayer? Which one if preferable?
I think you're on the wrong path here: you create a UIColor from a pattern (pattern already implies this is a repeating image). All in all- you can't have your pattern not repeat and centered.
If you just want simple image as background of your UIView, just add it as a subview and center it.
UIImage* img = [UIImage imageNamed:#"yourfile.png"];
UIImageView* imgView = [[UIImageView alloc]initWithImage: img];
[yourUIView addSubview: imgView];
imgView.center = CGPointMake(yourUIView.frame.size.width/2, yourUIView.frame.size.height/2);
Now - add more subviews to your "yourUIView" view and they'll show on top of the image - thus the image becoming the background.
So... no need to draw anything yourself.

Resources