I am comparing cell.imageView.image with UIImage as below code.
Please refer below Code
UIImage *imgOne = [UIImage imageNamed:#"One"];
if([customCell.imageView.image isEqual: imgOne]){
NSLog(#"Right Image");
}
While debugging, I write as po [customCell.imageView.image isEqual imgOne], but it always returns as 'nil'.
Can anyone tell me how to compare this or proper way to compare?
Generally UIImageView can not store file name of image but it store only UIImage so, it is not possible to get file name of image which you add in UIImageView.
Thats way if you want to compare only image file name then it is not possible to compare it.
But If you want to compare two UIImage object then
UIImage *secondImage = [UIImage imageNamed:#"image.png"];
NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqualToData:imgData2];
if(isCompare)
{
NSLog(#"Image View contains image.png");
}
else
{
NSLog(#"Image View doesn't contains image.png");
}
If you matain the "image one" as a member var.(1)
And set the cell's image this value.(2)
Then you can compare using those two pointers.(3)
See this code:
//1
self.imgOne = [UIImage imageNamed:#"One"];
//2
customCell.imageView.image=self.imgOne
//3
if(customCell.imageView.image == self.imgOne)
{}
Instead of Imageview I tried with Button
And than i Check as below
if([cell.button.currentImage isEqual: [UIImage imagenamed:"Your Imagenem"]]){
NSLog(#"Right Image");
}
It works fine..
Thank you.
Related
I am trying to switch between two different images when clicking on a button. Below is the code I wrote so far. However, this will only display one image and not toggle between them.
-(IBAction)show {
BOOL img = true;
if (img = true) {
UIImage *img1 = [UIImage imageNamed:#"UnCheck.png"];
[imageview setImage:img1];
}
else {
UIImage *img = [UIImage imageNamed:#"Check.png"];
[imageview setImage:img];
}
}
You have a couple of issues with your code:
First, img is a local variable inside your tap handler, so each time that function runs, it will be set to true.
Second, your if statement actually contains an assignment (=) instead of a comparison (==), so even if img wasn't already true it would be when you execute the if statement.
All of this means that your image is always going to be UnCheck.png.
You need to use a property, outside the function, so that the state is tracked properly. img is also a pretty poor variable name, checked or isChecked is probably better.
Then your button handler method simply needs to toggle this property and set the appropriate image.
#property BOOL isChecked;
-(IBAction)show {
self.isChecked = !self.isChecked;
NSString *imageName = self.isChecked ? #"Check.png":#"UnCheck.png";
UIImage *img = [UIImage imageNamed:imageName];
[imageview setImage:img];
}
I found a way to store images of UIImagePickerControllerSourceTypePhotoLibrary in an NSArray but I can not find a solution to view images in an UIImageView from the NSArray. Here is the link to store image:
Can the selected images from library be stored in an array?
You just need to iterate through that array...
for(UIImage *img in self.imageArray){
self.image = img;
}
Or imageView:
for(UIImageView *imgView in self.imageArray){
self.imageView = imgView;
}
Lets say you stored images in array :
NSArray *imageArray = #[image1, image2, image3]; //imageX are arbitrary name for images.
To view these images one by one, you need to load them and show in UIImageView:
//lets assume you have multiple buttons and all targeted to this method.
-(IBAction)buttonClicked:(UIButton *)sender{
NSInteger index = sender.tag;
//here index is the integer to load the image.
if(index < imageArray.count){
self.imageView setImage: imageArray[index];
}
}
This is my question:
I want to rotate my image when I got the degree it should rotate.
And my code is here
UIImage *image = imageView.image;
UIImage *originalImage = imageView.image;
CGAffineTransform transform = imageView.transform;
if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2))) {
image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationRight];
} else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI))) {
image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationDown];
} else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2 * 3))) {
image = [UIImage imageWithCGImage:originalImage.CGImage scale:originalImage.scale orientation:UIImageOrientationLeft];
} else if (CGAffineTransformEqualToTransform(transform, CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * 2))) {
image = originalImage; // UIImageOrientationUp
}
As I hope , the image will show as rotated. But after rotating, this images is still what it was. It means the method imageWithCGImage:scale:orientation: didn't work.
Some one can tell me why? Thanks.
You use
UIImage *image = imageView.image;
to get the handle of imageView.image, and you've set your changed image for (UIImage*)image which is just a reference of imageView.image.So if you want imageView.image to be change, you should set imageView.image again to make it change
add this in the back after your codes
imageView.image = image;
You need set the image again (after all that code)
imageView.image = image;
This is something that you will need to fundamentally understand. When you get a pointer to another object, and then set that pointer to something else, it doesn't affect the original object. Think of it this way:
Lunch *myLunch = myFriend.CurrentLunch;
This means that your lunch is current your friend's lunch.
myLunch = new MisoRamen();
Now your lunch is a different lunch.
myLunch.InsertWeirdSauce();
You just inserted a sauce into your own lunch, while your friend's lunch remains safe. If you want to change your friend's lunch you have to do this.
Lunch *newLunch = new MisoRamen();
newLunch.InsertWeirdSauce();
myFriend.CurrentLunch = newLunch;
Now your friend will gasp in shock as he/she eats a lunch with weird sauce in it.
I am trying to compare an image from a button with another image but its not working as expected and as stackoverflow suggestions...
if (favoriteButton.imageView.image == [UIImage imageNamed:#"favorite.png"]) {
NSLog(#"yes!");
}
anything else I can do? I tried to get the image file name but it seems to be impossible.
UIImage is a class, not a native type. You should use the isEqual: method, not the == operator to see if the two objects represent the same data.
if ([favoriteButton.imageView.image isEqual:[UIImage imageNamed:#"favorite.png"]]) {
NSLog(#"yes!");
}
Only use the == operator with object pointers if you really want to see if the two pointer reference the exact same location in memory.
I found this: How to compare two UIImage objects
this is what I tink may help:
- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqual:data2];
}
Reading the comments of your question, i think u cloud just assign the button a "state" such as "selected".
On the normal state the button cloud load the "not_favorite.png".If the user tap on the button it will be on a selected state and the corresponding image(favorite.png) will load.
On future if you need to know which image is loaded(or if the user already taped on the button to make it favorite) u can just check the button's state.
Personally i will not compare images just to fire an action for a button. It will be costly.
why not try to use imageView to identify the image?
UIImageView *imageView_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"favorite.png"]];
UIImageView *imageView_not_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"not_favorite.png"]];
//both imageView_favorite and imageView_not_favorite can be set as an member ivar, and the favoriteButton.imageView can be set to them appropriately.
if (favoriteButton.imageView == imageView_favorite) {
NSLog(#"yes!");
}
try this
must check your buttonname and extension. it must be same.
-(IBAction)btnMaleClick:(id)sender {
if (btnMale.imageView.image == [UIImage imageNamed:#"radiobtn.png"]) {
[btnMale setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
else {
[btnMale setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
}
}
I cache an image when the user logs in like this:
This is in a Utility.m
UIImage *image = [UIImage imageWithData:newProfilePictureData];
UIImage *mediumImage = [image thumbnailImage:280 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationHigh];
UIImage *smallRoundedImage = [image thumbnailImage:64 transparentBorder:0 cornerRadius:9 interpolationQuality:kCGInterpolationLow];
NSData *mediumImageData = UIImageJPEGRepresentation(mediumImage, 0.5); // using JPEG for larger pictures
NSData *smallRoundedImageData = UIImagePNGRepresentation(smallRoundedImage);
Later on I would like to load the small image into an UIImageView:
This is in a UITableView - cellForRowAtIndexPath
[cell.leftIcon setImage:
Does anyone know how I can reference the cached image?
You need something like below method in Utility.m,
+(NSString*)getImage {
return smallRoundedImage;
}
Then use it like below,
[cell.leftIcon setImage:[Utility getImage]];
PS: This is just an example, you can make this much better based on what's your requirements!