How to add multiple images to image view - ios

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

Related

How to rearrange UIImages?

i'm creating a app where i'm adding some UIImages as subview of UIview. Now if I delete image I want to rearrange the remaining UIImages.
How can I achieve this?
EDIT
This is what i have tried so far:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Add You all Images in NSMutableArray and declare NSMutableArray in .h file.
self.imagArray = [[NSMutableArray alloc] init];
[self.imagArray addObject:[UIImage imageWithName:#"image1.png"];
.
.
.
.
[self.imagArray addObject:[UIImage imageWithName:#"image_N.png"];
And you can easily manage it for add/delete image by self.imagArray.
If you want to delete any image then also write this code
[self.imagArray removeObjectsAtIndexes:indexes];
I assume you are using array of images. Just remove the index once it's deleted and have a login to refresh the view.
Init images to array first:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Delete image:
NSUInteger deleteIdx = [ImagesArray indexOfObject:deleteImage];
[deleteImage removeFromSuperview];
[self relayoutViews:deleteIdx];
- (void)relayoutViews:(int)deleteIdx{
for (int i=deleteIdx; i<[ImagesArray count]; i++) {
UIImageView *imageView = [ImagesArray objectAtIndex:i];
imageView.frame = CGRectMake(20, 100*i+100, 60, 60)];
}
}
With the help of tag you can get the imageview which user want to delete .So you have to add TapGesture on UIimageView and you have to bind method for deleting image.Like this
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:singleTap];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
- (void)oneTap:(UIGestureRecognizer *)gesture {
int myViewTag = gesture.view.tag;
// Now with the help of tag you can remove object from you array and which you are want to remove and then you can reload you view
}

Getting Screenshot of certain views to camera roll iOS

Hi I am still a beginner with Objective-C and am hoping someone can help me out. What I have created programmatically is a UIScrollView that has images inside that you can swipe across. Then I created a UIButton that when pressed is supposed to take a screenshot of the image subview the has been selected and then saves it to the camera roll (that part i'm not sure where to add in the code). The reason I want it to be a screenshot and not just the image is because I will later add UILabels on top of the images and I want those to appear in the saved screenshot as well. I am not quite sure which views to use in the saveWallpaper method and even if my current solution will work. I have looked at this answer Link to get the screenshot code to try for this implementation. Basically I want it to take a screenshot of everything besides the UIButton and save it to the camera roll. Any help or direction would be greatly appreciated. I could also try an approach where it takes a screenshot or captures certain elements and adds them together to be a single image. Thank you!
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 21;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png",#"4.png",#"5.png",#"6.png",#"7.png",#"8.png",#"9.png",#"10.png",#"11.png",#"12.png",#"13.png",#"14.png",#"15.png",#"16.png",#"17.png",#"18.png",#"19.png",#"20.png",#"21.png", nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos +=scroller.frame.size.width;
}
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton addTarget:self
action:#selector(saveWallpaper:)
forControlEvents:UIControlEventTouchDown];
[saveButton setImage:[UIImage imageNamed:#"save.png"] forState: UIControlStateNormal];
[saveButton setImage:[UIImage imageNamed:#"savepressed.png"] forState: UIControlStateHighlighted];
saveButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2, 396, 96.5, 42); //Make this in the center
[self.view addSubview:saveButton];
}
- (void)saveWallpaper:(id)sender
{
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(scroller.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(scroller.bounds.size);
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"foo.png" atomically:YES];
}
}
Rather than drawing the complete scroller view as in the following statement
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
Draw the selected image and the text using drawInRect method. See this related post for reference.

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];

Scrollview not shwoing images

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]];

Scrolling an Image with ImageView iOS

I'm trying to make an app for a restaurant and the to display the food menu as a scrollable image. The image is pulled form a config file.
- (void)viewDidLoad
{
[super viewDidLoad];
// back button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"btn-back.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 46.0f, 28.0f);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
[self setTitle:[menu objectForKey:#"title"]];
[scrollView2 setScrollEnabled:YES];
NSString *imageName = [menu objectForKey:#"imageName"];
if (![imageName isEqualToString:#""]) {
UIImage *image = [UIImage imageNamed:imageName];
[imageView setImage:image];
[scrollView2 addSubview:imageView];
[imageView release];
}
[textView setText:[menu objectForKey:#"description"]];
}
As of now the image doesn't scroll, but appears as a static image. Could anyone please help me modify this code so the user can vertically scroll the image.
Thanks a lot.
You should use a UIScrollViewand add the UIImageView as subview of the contentView.
And then set the contentsize of the scrollview as the imageviews size.
[imageView setImage:image];
[scrollView2.contentView addSubview:imageView];
[scrollView2 setContentSize:image.frame.size];
[imageView release];
have no Mac to test at the moment but that should work.
I agree mostly with what Seega wrote. Your imageView must be subview of a scrollView. Then, don't forget to set the scrollView's contentSize & the minimum&maximumZoomScale & and the imageView's frame:
self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.minimumZoomScale = // some number between 0 and 1, default is 1, so no zoom
self.scrollView.maximumZoomScale = // some number larger than 1, default is 1, so no zoom
self.scrollView.zoomScale = 1 // starting zoom scale
A nice place to put this code is in
-(void)viewWillLayoutSubviews{}
Thus it will be executed everytime the view appears & also whenever the device is rotated.

Resources