Every time you click on the create button 4 rows of images are created. I'm using this technique for the game mastermind. When you click on the clear button every imageview has to be set to nil, but now only the last created 4 imageviews are set to nil. Any help would be appreciated!
- (IBAction)create {
_imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(134, y, 30, 30)];
_imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(170, y, 30, 30)];
_imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(206, y, 30, 30)];
_imgView4 = [[UIImageView alloc] initWithFrame:CGRectMake(241, y, 30, 30)];
_imgView1.image = [UIImage imageNamed:[textfield_code objectAtIndex:0]];
_imgView2.image = [UIImage imageNamed:[textfield_code objectAtIndex:1]];
_imgView3.image = [UIImage imageNamed:[textfield_code objectAtIndex:2]];
_imgView4.image = [UIImage imageNamed:[textfield_code objectAtIndex:3]];
[self.view addSubview:_imgView1];
[self.view addSubview:_imgView2];
[self.view addSubview:_imgView3];
[self.view addSubview:_imgView4];
y += 41;
}
- (IBAction)clear {
[_imgView1 setImage:nil];
[_imgView2 setImage:nil];
[_imgView3 setImage:nil];
[_imgView4 setImage:nil];
}
enumerate the view's subviews
for(UIImageView *v in self.view.subviews) {
if([v isKindOfClass:[UIImageView class]) {
[v setImage:nil];
}
}
BUT this is 'dangerous' as it removes ANY image view there is. it'd be better to use tags too.
e.g.
define it as #define MyTag 100
and when will creating v.tag = MyTag
and then in the loop check it if(v.tag == MyTag)
Related
I am trying to save the tapped image to a photo album, using contentOffset to detect which image object is tapped to save, but it always saves the last imageObject instead.
Here is how I try to calculate contentOffset for tapped image view in scrollView:
(void)viewDidLoad
{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {
CGFloat xOrigin = i * 320;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
_imageView = [[[UIImageView alloc] initWithImage:image]autorelease];
_imageView.frame = CGRectMake(xOrigin, 0, 320, 480);
_imageView.tag = i;
[_imageScrollView viewWithTag:i+1];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(320 * 61 , 480);
[self.view addSubview:imageScrollView];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[self performSelector:#selector(LongPress:) withObject:nil];
break;
default:
break;
}}
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
CGPoint offset = _imageScrollView.contentOffset;
int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
UIImage* image = [(UIImageView*)[_imageScrollView viewWithTag:imageView] image];
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
I expected this code to save the tapped image, but it saves the last image view from the scrollview instead.
Please let me know if I am doing it incorrectly, and if I am still missing something. Thanks for the help.
The reason is in your viewDidLoad you are using
_image = [UIImage imageNamed:imageName];
I think this is a global variable in the class, and you are using this object to save the image to photo album. As it is a global variable in the last count of your loop's image will be pointing to it, so you are always getting last image saved.
You can fix it by making _image local within for loop. And as you are setting the tag, using the tag you can get the imageView/image from scrollView. You can have like this
//your imageView in longPress function has the selected imageView's tag
UIImage* selImage = [(UIImageView*)[imageScrollView viewWithTag:imageView] image];
This should work for you.
int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
By this line
int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
I want to create a dial and i want to get the tag of images when clicked the images in the dial.i tried a lot , but i didn't get it.see the code below
- (void) drawWheel {
container = [[UIView alloc] initWithFrame:self.frame];
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int i = 0; i < numberOfSections; i++) {
UIImageView *im = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"segment.png"]];
im.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
im.layer.position = CGPointMake(container.bounds.size.width/2.0-container.frame.origin.x,
container.bounds.size.height/2.0-container.frame.origin.y);
im.transform = CGAffineTransformMakeRotation(angleSize*i);
im.alpha = minAlphavalue;
im.tag = i;
if (i == 0) {
im.alpha = maxAlphavalue;
}
cloveImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 40, 40)];
cloveImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"icon%i.png", i]];
cloveImage.tag=i;
[im addSubview:cloveImage];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//i want to get cloveImage.tag when i clicked the imageview
}
So my question is how can I detect tag of image in the rotatory wheels when i touched the
particular image?
i suggest you add buttons instead on UIImageView and then add background image to the uibuttons.As far as getting the tag is concerned you shall get the corresponding tag of clicked buuton in its target method
for (UIImageView *img in self.view.subviews)
{
if ([img isKindOfClass:[UIImageView class]])
{
if (img.tag==index)
{
Your code....
}
}
}
You use the above code for getting tha tag of Perticular image by using for loop..
try this:
... ...
cloveImage = [[UIImageView alloc] initWithFrame:CGRectMake(12, 15, 40, 40)];
cloveImage.image = [UIImage imageNamed:[NSString stringWithFormat:#"icon%i.png", i]];
cloveImage.tag=i;
// **********add begin**********
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showCloveImageTagOnImageView:)];
[cloveImage addGestureRecognizer:tapGR];
// **********add end**********
[im addSubview:cloveImage];
... ...
// copy the selector below and log the cloveImageTag
- (void)showCloveImageTagOnImageView: (UIImageView *)tappedImageView
{
NSLog(#"tappedImageView.tag: %d", tappedImageView.tag);
}
I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.
I want to draw a rectangle on one image
how to do that?
Start by reading Quartz 2D Programming Guide and go to paths section.
-(IBAction)Handeltap:(UIGestureRecognizer *)sender
{
if(sender.state==UIGestureRecognizerStateBegan)
{
tapPoint1 = [sender locationInView:sender.view];
NSLog(#"UIGestureRecognizerStateBegan and x=%d and y=%d",(int)tapPoint1.x,(int)tapPoint1.y);
img1=[[UIImageView alloc]initWithImage:nil];
[img1 setBackgroundColor:[UIColor lightTextColor]];
CGRect rect1=CGRectMake((float)tapPoint1.x,(float)tapPoint1.y,50,20);
NSLog(#"rect=%f and %f and %f and %f",rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height);
[img1 setFrame:rect1];
[self.view addSubview:img1];
[self.view bringSubviewToFront:img1];
}
if(sender.state==UIGestureRecognizerStateChanged)
{tapPoint2 = [sender locationInView:sender.view];
// NSLog(#"UIGestureRecognizerStateChanged and x=%d and y=%d",(int)tapPoint.x,(int)tapPoint.y);
[img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)];
}
if(sender.state==UIGestureRecognizerStateEnded)
{
tapPoint2 = [sender locationInView:sender.view];
NSLog(#"UIGestureRecognizerStateEnded and x=%d and y=%d",(int)tapPoint2.x,(int)tapPoint2.y);
[img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)];
}
}
add tap gesture and paste this
UILongPressGestureRecognizer *ges11=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(Handeltap:)];
[self.view addGestureRecognizer:ges11];
Tanks for helping me..
If you just want to add a simple colored rectangle to an image you can do it like this:
UIImage *imageFile = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"myImageFile" ofType:#"jpg"]];
UIImageView *myImageFileView = [[UIImageView alloc] initWithImage:imageFile];
myImageFileView.frame = CGRectMake(0, 0, 320, 480); //size of image view
[self.view addsubview:myImageFileView]; //I assume this is within a view controller .m
UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; //A rectangle at point (0, 0) 50x50 in size.
rectangle.backgroundColor = [UIColor redColor]; //color the rectangle
[myImage addSubview:rectangle]; //add the rectangle to your image
[rectangle release];
[imageFile release];
[myImageFileView release];
I try to make following: Horizontal list of images, that I can select and do something with it. For example flip image around x axis. I know how to rotate image. I created scrollview and load it with images. I added event handler when I tap on image. But I don't know how to do something with tapped image. How to code method to do something with tapped image?
- (void)viewDidLoad {
[super viewDidLoad];
img1 = [UIImage imageNamed:#"imgTest.jpg"];
img2 = [UIImage imageNamed:#"imgTest2.jpg"];
arrayOfImages = [[NSMutableArray alloc] init];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
[arrayOfImages addObject:img1];
[arrayOfImages addObject:img2];
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 128);
[self.view addSubview:scrollView];
UIImage *imageToAdd;
int x = 0;
int y = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 128, 128);
temp.userInteractionEnabled = YES;
x += 135;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[temp addGestureRecognizer:tap];
[scrollView addSubview:temp];
}
...
- (void)imageTapped:(UIGestureRecognizer *)sender
{
// how to get reference on selected item in scrollview???
}
A gesture recognizer has a view property that returns the view associated with the recognizer. Since you know that it'll be a UIImageView you can simply cast it and use it as in:
UIImageView *iv = (UIImageView *)[sender view];
And your image can then be queried via:
UIImage *image = [iv image];
If you need to know the index in your array, there are two ways: either simply use [arrayOfImages indexOfObject:image];, or you can assign tags (numbers) to views and use them. A tag is not used by Apple, it's only here so we developers can "mark" views in some way. For example:
NSInteger counter = 0;
for(imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
count.tag = counter++;
...
}
Then, in your imageTapped:, you can query the index via tag:
NSInteger index = [imageView tag];