How to remove overlapping uiimageview? - iphone

I tried to change the image in the imageview but I can't seem to do that.
My code shown below creates a new subview for each image to display. However, the images just overlap and shows the overlapped images at the end which is not supposed to be.
.m:
for (int i = 0; i < [imageArr count]; i++) {
NSArray *imageNameArr = [[imageArr objectAtIndex:i] componentsSeparatedByString:#"."];
check=line;
NSString *msg = [textView.text stringByAppendingString:[NSString stringWithFormat:#"Opening image: %#\n",[imageArr objectAtIndex:i]]];
[textView performSelectorOnMainThread:#selector(setText:) withObject:msg waitUntilDone:NO];
line=line+1;
NSString *imagePath = [NSString stringWithFormat:#"%#/%#",jName,[imageArr objectAtIndex:i]];
NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];
NSLog(#"------------------------------");
NSLog(#"ImageFile: %#\n",imageFile);
NSLog(#"Interger value: %i\n",i);
UIImage *myImage = [UIImage imageWithContentsOfFile:imageFile];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:imageView];
[imageView release];
.....
.....
}
How to show the images individually, without overlapping?

put this line out of for loop:
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
[scrollView addSubview:imageView];
and use it like:
UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];
and use:
[imageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];
Inside for loop.

You create the imageview out of loop and change the source(Image) for that imageview inside the loop.
You do this before loop starts,
UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];
and add the image like this inside the loop,
[imageView setImage:[UIImage imageWithContentsOfFile:imageFile]];

Related

Save image from scrollview to photo album

How to save image from scrollview to photalbum when i m using
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
in scrollview to display image view.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(_imageScrollView._image, self, nil, nil);
break;
default:
break;
}
}
Getting message in red on statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView._image, self, nil, nil);
that property _image not found on object of type uiscrollview.
Thanks for help.
You can hang on to the image with a separate pointer, or if the scroll view is the only pointer to it, you can find it with a tag:
// right after
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// give it a tag
imageView.tag = 128; // any int that is unique relative to other tags in the scroll view
// find it with the tag...
case 0:
UIImageView *imageView = (UIImageView *)[imageScrollView viewWithTag:128];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, nil, nil);
// notice, there's also no property called _image on an imageView

Replacing image in UIScrollView on selected item

I created simple UIScrollView that have images. Each time an image pressed i want to change that image, how can I do it?
I creating this UIScrollView and initializing it with NSMutableArray with images.
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)];
NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil];
for (int i=0; i<3; i++)
{
UIImageView *imageV = [UIImageView alloc];
[imageV setImage:[images objectAtIndex:i]];
[myScroll addSubview:imageV];
[imageV release];
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector (changeImg:)];
[myScroll addGestureRecognizer: singleTap];
and the toucing I'm cathing with the touched place on the scroll:
- (void) singleTapGestureCaptured:(UITapGesturerecongnizer *) gesture
{
CGPoint touch = [gesture locationInView:myScroll];
}
By X,Y of touched item i know what image was selected
Here I need to change for example the first image of myScroll...How can i do it?
Add UITapGestureRecognizer on UIImageView and set its userInractionEnabled: YES which is NO by default for UIImageView.
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)];
NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil];
for (int i=0; i<3; i++)
{
//In your question you didn't set `imageView` frame so correct it
UIImageView *imageV = [[UIImageView alloc]initWithFrame:yourFrame];
[imageV setImage:[images objectAtIndex:i]];
[imageV setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:#selector (changeImg:)];
[imageV addGestureRecognizer: singleTap];
[myScroll addSubview:imageV];
[imageV release];
}
After adding all imageView successfully you get them click like this :-
-(void)changeImg:(id)sender
{
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
UIImageView *imageView = (UIImageView *)recognizer.view;
[imageView setImage:[UIImage imageNamed:#"anyImage.png"]];
}
Your application will crash because you are accessing the index 3, but there is not any object at index 3.
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)];
NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil];
[myScroll addSubview:[images objectAtIndex:0];
[myScroll addSubview:[images objectAtIndex:1];
[myScroll addSubview:[images objectAtIndex:2];
Now you can access the image view with tagValue
-(void)changeImg :(UIGestureRecognizer*)recog
{
UIScrollView *scroll = (UIScrollView*)recog.view;
CGPoint point = scroll.contentOffset;
int imagetag = (point.y/scroll.frame.size.height);
UIImageView *image=(UIImageView*)[[scroll subviews] objectAtIndex:imagetag];
NSLog(#"Image tag = %d",image.tag);
image.image=[UIImage imageNamed:#"Icon-72.png"];
}
A simple implementation will be, if you use a custom button and change its image on its IBAction.. Still you can addGesture to ur UIImageView too and on the
if(recognizer.state == UIGestureRecognizerStateBegan)
you can change the image on runtime.
I hope this helps. Cheers!!

Display a serial of UIImageViews using the UIImage objects of a NSArray

I can not find out a easy way. Anyone help? Thanks!
Put your image name into array instead of images, that's much better memory-wise.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
CGFloat currentOffset = 0;
for (int i = 0; i < [imageArray count]; i++) {
UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
currentOffset += tmpImageView.frame.size.width;
//currentOffset += tmpImageView.frame.size.height; // for vertical
tmpImageView.frame = CGRectOffset(tmpImageView.frame, currentOffset, 0);
//tmpImageView.frame = CGRectOffset(tmpImageView.frame, 0, currentOffset); // for vertical
[scrollView addSubview:tmpImageView];
[tmpImageView release];
}
scrollView.contentSize = CGSizeMake(currentOffset, scrollView.frame.size.height);
//scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, currentOffset); // for vertical
Then add that scrollView to shichever view you want and release it.
As you can see this will put all your images one next to other horizontally. If you want to do it vertically, simply uncomment all the commented lines and delete the line before each of those commented lines.
This is how one would solve this problem with iOS4. iOS5 adds an easier way to do this but you'll need to look at the doc since it's still under NDA.
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"one.png"],
[UIImage imageNamed:#"two.png"],
[UIImage imageNamed:#"three.png"],
[UIImage imageNamed:#"four.png"],
nil];

error while setting image in imageView in iphone

I have written a code for setting imageview ...as per the button clicked...but my problem is when i press the button it is giving me the error like...too many arguments to function ImageNamed...while executing this line...
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
the whole code is....
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
UIImage* photo = [UIImage imageNamed:#"robort %d.jpg",x];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView]; }
use UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort de nero%d.jpg",x]];
cheers :)
Try this
-(void)buttonClicked:(UIButton*)button
{
NSLog(#"%d",button.tag);
int x;
x=button.tag;
// Create the image view.
NSString *imageName = [NSString stringWithFormat:#"robort %d.jpg",x];
UIImage* photo = [UIImage imageNamed:imageName];
NSLog(#"%d",x);
CGSize photoSize = [photo size];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[imageView setImage:photo];
[self.view addSubview:imageView];
}
You need to use:
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];
Use this
UIImage* photo = [UIImage imageNamed:[NSString stringWithFormat:#"robort %d.jpg",x]];

UIButton doesn't interact in UIScrollView

I have a windowed (e.g. not a full-screen) UIScrollView (inside a UIView) which scrolls through groups of UIImageViews with UIButtons on them (the idea being you click the button to do something with the displayed image). The UIButton does take any touch events - how can I fix it?
I've read this, this, this, this and this - but I either don't understand the answer or its implications, or it's not really relevant.
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i];
//NSLog(#"testSentence: %#", testSentence);
//NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
//Your going to need to optimise this by creating another thumbnail image to use here.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]];
//NSLog(#"imageName: %#", imageName);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName];
//NSLog(#"image: %#", image);
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//NSLog(#"imageView: %#", imageView);
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setBackgroundColor:[UIColor blackColor]];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:#"Play" forState:UIControlStateNormal];
aButton.frame = CGRectMake(10, 10, 70, 70);
[aButton setUserInteractionEnabled:YES];
[aButton setEnabled:YES];
[aButton setAlpha:1];
UIView *buttonWrapperUIView = [[UIView alloc] init];
[aButton addTarget:self action:#selector(clickPlay:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];
[imageView bringSubviewToFront:aButton];
[scrollView1 addSubview:imageView];
NSLog(#"aButton %#", aButton);
[image release];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
try
[imageView setUserInteractionEnabled:YES]