Scrollview not shwoing images - ios

i have retrived images from document directory and want to display that images in scrollview. the images is coming from the document directory but not displaying in scrollview. i have the below code. I have put scrollview graipically on xib and also creat outlet..
self.scrollView.contentSize = CGSizeMake(320, 136);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
//imge.image=img;
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 100, 100)] ;
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 140);
}

Seems you are trying to create the UIImageView from images that are located in your resources (but they are not there!).
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
whats the point of placing the images in a array, if you dont use it?
try this instead:
[imageView setImage: [images objectAtIndex:i]];

Related

How to add multiple images to image view

i have a view i added text and images on that view. i want that if i added one image then at the same time i also added next and next image, but i have problem in that. when i move one image in that specific view then the other images also there not to be disappeared.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imageView.hidden =NO;
self.imageView .image = image;
[txtNotes addSubview:self.imageView];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Try this to show images on imagesView....
UIImageView *imageView;
NSArray *arrayOfImage=[[NSArray alloc]initWithObjects:#"i1.jpg",#"i2.jpg",#"i3.png", nil];;
float x=10;
float y=50;
float width=100;
float height=100;
int i=0;
for(i=0;i<[arrayOfImage count];i++)
{
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x+width*i, y, width, height)];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
}
else if(i==[arrayOfImage count])
{
i=0;
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
i++;
}
if you want to change the I'm image on next button and back button... then try below code
- (IBAction)btnClicked:(id)sender
{
UIImageView *imageView;
NSArray *arrayOfImage=[[NSArray alloc]initWithObjects:#"i1.jpg",#"i2.jpg",#"i3.png",#"i1.jpg",#"i2.jpg",#"i3.png", nil];;
float x=100;
float y=100;
float width=100;
float height=100;
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
if([sender tag]==0)
{
if(i<[arrayOfImage count])
{
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
i++;
}
else if(i==[arrayOfImage count])
{
i=0;
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
i++;
}
}else
{
if(i<[arrayOfImage count])
{
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
i--;
}
else if(i==-1)
{
i=0;
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrayOfImage objectAtIndex:i]]];
[self.view addSubview:imageView];
i++;
}
}
}
Note: set Tag to Next button as 0 and set tag to back button as 1.
Use multiple UIImageViews and before placing them using addSubview, set them a frame, which will be different for every UIImageView.
Putting multiple images into one UIImageView is certainly possible, but achievable probably only by merging images into one, which I would not consider the best option. How to do that, you can find in accepted answer there:
iOS - Merging two images of different size

UIImage animatedImageNamed repeat count

I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];

Images are not properly displaying on UIScrollView

I have images in a UIScrollView, but the images are not displaying properly. The problem is that images are not at the same size. I want every image with the same size and height.
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
//CGRect workingFrame = _scrollView.frame;
CGRect workingFrame = CGRectMake(0, 0, 200, 134);
workingFrame.origin.x =0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
If you set UIViewContentModeScaleAspectFit you will have the image with the maximum size of the UIImageView, but often smaller. If you want it to fill and have all the same size and keep aspect you have to set:
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setClipsToBounds:YES];

Image in full screen from scroll view

I have a UIScrollView and have images in that ScrollView and they are added programmatically. When I click on image, I want that image to be displayed on full screen.
What should I add to my code?
The number of images depend upon the [images count];
self.scrollView.contentSize = CGSizeMake(320, 134);
self.scrollView.delegate = self;
int X=0;
for (int i = 0; i < [images count]; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 140, 136)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:[NSString stringWithFormat:#"%d.png", i]]];
[imageView setImage: [images objectAtIndex:i]];
[self.scrollView addSubview: imageView];
X = X + imageView.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
You should not add UIImageViews then.
Instead add UIButton of type Custom and add the Image to the Button and add an IBAction to the UIButtons which adds a FullScreenView with your image.
In your for() for each button add a tag with i and in your IBAction function get the tag with [sender tag] and get your image with [images objectAtIndex:[sender tag]]
Example:
-(void)test{
for (int i = 0; i < [images count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(X, 0, 140, 136);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(showFullscreen:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
X = X + button.frame.size.height+5;
if(X > 320)
self.scrollView.contentSize = CGSizeMake(X, 134);
}
}
-(IBAction)showFullscreen:(id)sender{
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)] ;
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[self.view addSubview: imageView];
}

How can i add image names into an array?

In my app, i have a little problem in memory issue.
My coding skill is not enough to perfect.
My code use memory four time that i really need.
How can i change my coding?
My Code is
-(void)viewDidAppear:(BOOL)animated
{
UIScrollView * ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];// 44(UpperNavigtionBar),-88(Upper+Lower)
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
UIImage *image5 = [UIImage imageNamed:#"Image-005.jpg"];
UIImage *image6 = [UIImage imageNamed:#"Image-006.jpg"];
UIImage *image7 = [UIImage imageNamed:#"Image-007.jpg"];
UIImage *image8 = [UIImage imageNamed:#"Image-008.jpg"];
UIImage *image9 = [UIImage imageNamed:#"Image-009.jpg"];
UIImage *image10 = [UIImage imageNamed:#"Image-010.jpg"];
UIImage *image11 = [UIImage imageNamed:#"Image-011.jpg"];
UIImage *image12 = [UIImage imageNamed:#"Image-012.jpg"];
UIImage *image13 = [UIImage imageNamed:#"Image-013.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,nil];
NSInteger numberOfViews = 13;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView * ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height-88); // -88(for adding Image View as subview)
[self.view addSubview:ScrollView];
}
There are several things you can do to limit your memory usage. First, you should fill your array with the names of the images, not the images themselves. Given the way your images are named, you should be able to do this in a loop. Second, when you load the image, use imageWithContentsOfFile: instead of imageNamed:. The latter method caches images, while the former doesn't. If you use imageNamed: the memory usage will continue to grow as you scroll through images (even with lazy loading) because of the caching -- at some point the system should purge the cache, which should keep your app from crashing, but I don't think it will keep the system for shutting down other programs that are in the background on a device.

Resources