Ios image collision with one imageview - ios

I am making an app that has 2 UIImageViews but in each image view the image changes 3 times. How can I make it so that if image A collides with image B or C action happens but not with image A and so on. Here is the code for the image changing
ImageView Changing 1
This is a random change
Trap.center = CGPointMake(350,220);
Trap1 = rand() %3;
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
break;
default:
break;
}
ImageView Changing 2
This is set with a swipe and is copied for the other 2 changes.
-(IBAction)Change3:(id)sender{
UIImage *img = [UIImage imageNamed:#"Image4.tif"];
[Change3 setImage:img];
}

You can add a tag to your Trap object. Compare the tags and if they are different, do something.
Set tag:
switch (Trap1) {
case 0:
Trap.image = [UIImage imageNamed:#"Image1.png"];
Trap.tag = 1;
break;
case 1:
Trap.image = [UIImage imageNamed:#"Image2.png"];
Trap.tag = 2;
break;
case 2:
Trap.image = [UIImage imageNamed:#"Image3.png"];
Trap.tag = 3;
break;
default:
break;
}
Compare tags
If (trap1.tag != trap2.tag){
//Do something
}

Related

iOS how can I make undo action for rotating, scaling and adding UIView to UIIMageView

I have a UIImageView and I can add new items (as a subview) to this UIImageView or I can move & rotate the items in UIImageView.
I'm try to make undo actions for this items, so my code;
Before each new actions I get the copy of UIImageView and store them in NSMutableArray
[self.lastActions addObject:[self getCopy]];
I get the same UIImageView with different memory address. (I'm not sure this is the right approach)
-(UIImageView *)getCopy{
NSData *archivedViewData = [NSKeyedArchiver archivedDataWithRootObject:self.imageView];
id clone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedViewData];
return clone;
}
And when I make the undo;
-(IBAction)undoButtonClicked:(id)sender{
UIImageView *lastImageView = [self.lastActions lastObject];
[UIView animateWithDuration:0.1 animations:^{
self.imageView = lastImageView;
}];
[self.lastActions removeObject:lastImageView];
}
But it does not change the UIImageView on screen.
Any ideas for whats wrong on my code ?
I am try your above idea. Not able to get the output. this is another idea to achieve the same.
I am using image to reveal the image change on the same imageview "undo" operation. I have implemented like below method.
its working fine in my side.
I hope this will help us.
// Undo button action
-(IBAction)undoButtonClicked:(id)sender{
UIImage *lastImageView = lastImageView = (UIImage *)[lastActions lastObject];
if( [lastActions count]!=0) {
[UIView animateWithDuration:0.2 animations:^{
self.imageView.image = lastImageView;
}];
[lastActions removeObject:lastImageView];
}
}
// Copy the image from public variable self.imageview
-(IBAction)copyButton:(id)sender{
[lastActions addObject:self.imageView.image];
}
// Random Orientation change to rotate the image.
-(UIImageOrientation )randonImageOrientation {
int min = 0 ;
int max = 3;
UIImageOrientation ori;
int randomNumber = min + rand() % (max-min);
switch (randomNumber)
{
case 0 :
ori = UIImageOrientationDown;
break;
case 1:
ori = UIImageOrientationLeft;
break;
case 2:
ori = UIImageOrientationRight;
break;
case 3:
ori = UIImageOrientationUp;
break;
default :
ori = UIImageOrientationUp;
break;
}
return ori;
}
// below method using to rotate the image
-(UIImage *)imageOrientation :(UIImageOrientation)imageOri image:(UIImage *)oriImage {
UIImage * LandscapeImage = oriImage;
UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
scale: 0.5
orientation: imageOri];
return PortraitImage;
}

IOS drawing App, change buttoncolor

i made a simple drawing App, with a toolpanel which contains a pencil, a paint brush and an eraser. There a 12 different colors to draw with.
Now, i would like the pencil and the paint brush image, change in the color of the active color from the colorpanel. How do I do that?
Something like;
When toolbutton:pencil is pressed, color pencil (image) is color:changecolor.
How to translate this in code?
This is my code for the buttons.
- (IBAction)colorChange:(id)sender
{
UIButton *PressedButton = (UIButton*)sender;
switch (PressedButton.tag) {
case 0:
self.drawingView.lineColor = RGB(32, 152, 188);
break;
case 1:
self.drawingView.lineColor = RGB(33, 114, 177);
break;
case 2:
self.drawingView.lineColor = RGB(65, 79, 155);
break;
case 3:
self.drawingView.lineColor = RGB(110, 58, 141);
break;
case 4:
self.drawingView.lineColor = RGB(195, 26, 126);
break;
case 5:
self.drawingView.lineColor = RGB(228, 36, 40);
break;
case 6:
self.drawingView.lineColor = RGB(234, 98, 36);
break;
case 7:
self.drawingView.lineColor = RGB(241, 141, 33);
break;
case 8:
self.drawingView.lineColor = RGB(252, 199, 19);
break;
case 9:
self.drawingView.lineColor = RGB(255, 240, 79);
break;
case 10:
self.drawingView.lineColor = RGB(139, 188, 63);
break;
case 11:
self.drawingView.lineColor = RGB(0, 144, 92);
break;
}
}
- (IBAction)toolChange:(id)sender
{
UIButton *toolButton = (UIButton*)sender;
switch (toolButton.tag){
case 0:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 7;
self.drawingView.lineAlpha = 1.0;
self.potlood02.hidden=NO;
self.kwast02.hidden=YES;
self.gum02.hidden=YES;
break;
case 1:
self.drawingView.drawTool = ToolTypePen;
self.drawingView.lineWidth = 20;
self.drawingView.lineAlpha = 0.67;
self.potlood02.hidden=YES;
self.kwast02.hidden=NO;
self.gum02.hidden=YES;
break;
case 6:
self.drawingView.drawTool = ToolTypeEraser;
self.drawingView.lineWidth = 20;
self.potlood02.hidden=YES;
self.kwast02.hidden=YES;
self.gum02.hidden=NO;
break;
}
}
To change the colour of your pencil image you could do something like this
UIImage *originalImage = [UIImage imageNamed:#"PencilImage.png"];
UIImage *newImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)]; // your pencil image size
imageView.tintColor = [UIColor redColor]; // or whatever color that has been selected
imageView.image = newImage;
Hope this helps.

Display array of Images in UIImageView

array of images added into Project
imaging is the UIImageView and imagg is Image choosen by user its default
_imaging.image=imagg;
arr = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpeg"],
[UIImage imageNamed:#"3.jpeg"],
[UIImage imageNamed:#"4.jpeg"],
[UIImage imageNamed:#"5.jpeg"],
nil];
assume user selected third image i.e 3.jpeg i am showing the 3.jpeg in imageview. Then using two button actions respectively Next and Previous next or previous images 3.jpeg should display.
- (IBAction)previous:(id)sender {
////
}
- (IBAction)next:(id)sender {
//////
}
}
for the next action its starting from 2nd image even for previous action its starting from 1st image.
this is the simple concept, this is working in static method, but working fine
assign the one global variable
#property (strong,nonatomic) NSString *imagestr;
- (IBAction)previous:(id)sender {
int str=[imagestr integerValue];
NSLog(#"the int value==%d",str);
switch (str) {
case 0:
_imaging.image=[arr objectAtIndex:4];
imagestr=#"4";
break;
case 1:
_imaging.image=[arr objectAtIndex:0];
imagestr=#"0";
break;
case 2:
_imaging.image=[arr objectAtIndex:1];
imagestr=#"1";
break;
case 3:
_imaging.image=[arr objectAtIndex:2];
imagestr=#"2";
break;
case 4:
_imaging.image=[arr objectAtIndex:3];
imagestr=#"3";
break;
default:
break;
}
}
- (IBAction)next:(id)sender {
int str=[imagestr integerValue];
NSLog(#"the int value==%d",str);
switch (str) {
case 0:
_imaging.image=[arr objectAtIndex:1];
imagestr=#"1";
break;
case 1:
_imaging.image=[arr objectAtIndex:2];
imagestr=#"2";
break;
case 2:
_imaging.image=[arr objectAtIndex:3];
imagestr=#"3";
break;
case 3:
_imaging.image=[arr objectAtIndex:4];
imagestr=#"4";
break;
case 4:
_imaging.image=[arr objectAtIndex:0];
imagestr=#"0";
break;
default:
break;
}
}
On clicked on next button
Before using this first set currentIndex in this
-(IBAction)next{
if (arr.count > 0) {
currentIndex++;
if (currentIndex > arr.count - 1) {
currentIndex = 0;
}
imageViewObj.image = [arr objectAtIndex:currentIndex];
}
}
on pervious button
-(IBAction)pervious{
if (arr.count > 0) {
currentIndex--;
if (currentIndex <= 0) {
currentIndex = arr.count - 1;
}
imageViewObj.image = [arr objectAtIndex:currentIndex];
}
}
Try this is working fine
Set some integer property currentIndex to 2 (if you load first the 3rd image).
When calling the next/previous methods increment/decrement that property and load the correct image. ImageView *image = [arr objectAtIndex:currentIndex]
when you initialize your view controller, keep track of the current image being viewed for example 0, to be used as an array index.
When next is pressed, increment the index and retrieve the image from the array using the updated count.
e.g.
- (IBAction)next:(id)sender {
imageCount++;
[myImageView setImage:[UIImage imageNamed:arr[imageCount] ];
}
hope this helps. :)

iOS: How to flip image based on current orientation?

I am trying to check the current orientation of an image and flip it to the opposite of what it currently is.
This code works if I only have 1 element on my screen, but as soon as i increase the array its a 50/50 chance of getting the orientation right.
-(IBAction)flipBtn:(id)sender
{
UIImage* flippedImage;
if(flipBtn.tag==FLIP_BUTTON_TAG)
{
UIImage* sourceImage1 = self.outletImageView.image;
flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUpMirrored];
flipBtn.tag = FLIP_VUTTON_TAG2;
}else{
UIImage* sourceImage1 = self.outletImageView.image;
flippedImage = [UIImage imageWithCGImage:sourceImage1.CGImage scale:1.0 orientation: UIImageOrientationUp];
flipBtn.tag = FLIP_BUTTON_TAG;
}
NSInteger index1 = [imgViewArray indexOfObject:self.outletImageView];
[imgViewArray removeObject:self.outletImageView];
self.outletImageView.image = flippedImage;
[imgViewArray insertObject:self.outletImageView atIndex:index1];
}
Instead of: [imgViewArray removeObject:self.outletImageView];
Write: [imgViewArray removeObjectAtIndex:index1];
And afterwards you need to check flippedImage.imageOrientation == UIImageOrientationUp to see if it's inverted or not and make the appropriate change.
You can verify which orientation the picture has by accessing the info dictionary [info objectForKey:#"Orientation"]
Here is a method that will return the new desired orientation based on your previous check (you'd pass flippedImage.imageOrientation as the argument):
- (UIImageOrientation)orientation:(int)orientation
{
UIImageOrientation newOrientation;
switch (orientation)
{
case 1:
newOrientation = UIImageOrientationUp;
break;
case 2:
newOrientation = UIImageOrientationUpMirrored;
break;
}
return newOrientation;
}

Statement requires expression of integer type (int invalid) error with switch statement

I am trying to load an image and label corresponding to the table view row/cell selected by the user. The code appears in my DetailViewController implementation file as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a couple of UIImages
UIImage *jenImage = [UIImage imageNamed:#"102_2262.jpg"];
UIImage *jen2Image = [UIImage imageNamed:#"102_2288.jpg"];
UIImage *paigeImage = [UIImage imageNamed:#"102_2332.jpg"];
UIImage *parkerImage = [UIImage imageNamed:#"102_2379.jpg"];
//Switch UIImages and UILabel based on characterNumber
switch (characterNumber) {
case 0:
characterName.text = #"Casual Jen";
characterPic.image = jenImage;
self.title = #"Casual Jen";
break;
case 1:
characterName.text = #"Dressed Up Jen";
characterPic.image = jen2Image;
self.title = #"Dressed Up Jen";
break;
case 2:
characterName.text = #"Paige";
characterPic.image = paigeImage;
self.title = #"Paige";
break;
case 3:
characterName.text = #"Parker";
characterPic.image = parkerImage;
self.title = #"Parker";
break;
default:
characterName.text = #"Parker";
characterPic.image = parkerImage;
self.title = #"Parker";
break;
}
}
The switch statement produces the error as described. I have searched for the solution here and elsewhere without success. Any help is greatly appreciated!
Make the data type of characterNumber as int.

Resources