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];
}
}
Related
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.
I'm having trouble with loops. I have one imageView which displays an artist's image and another imageView which I would like to display artwork of a random song. BUT, this artwork must not match the artist image. [To clear things up - the artist image is one of the song's artwork].
I'd like to essentially write this in Objective-C for iPhone:
Do {
Get artwork of random song in songsArray
Until{
image1 != image2;
}
This is what I've tried so far (firstArtwork is the artist's image and secondArtwork is the randomly-chosen artwork that must not be the same as firstArtwork).
-(UIImage *)getSecondImage{
MPMediaItemArtwork *firstArtwork = [self.mediaItem valueForProperty:MPMediaItemPropertyArtwork];
// Choose random song in songsArray + get Artwork
MPMediaItem *myItem = [songTracks objectAtIndex:arc4random() % songsArray.count];
MPMediaItemArtwork *secondArtwork = [myItem valueForProperty:MPMediaItemPropertyArtwork];
// Get images
UIImage *firstImage = [firstArtwork imageWithSize:CGSizeMake(1, 1)];
UIImage *secondImage = [secondArtwork imageWithSize:CGSizeMake(1, 1)];
// IF the two images are the same, repeat this block
// IF NOT, return secondImage
if ([firstImage isEqual:secondImage]){
[self getSecondImage];
}
else{
return secondImage;
}
return secondImage;
}
But this sometimes returns an image that's the same as the artist's image.
I've also tried using an NSPredicate but I get the error that you can't filter using the MPMediaItemPropertyArtwork.... so I can't use a preciate.
I'm not sure if I'm going about this logically/the correct way. Can anybody help me out and explain where I'm going wrong?
I think the problem is with your return value. Note that your if statement that checks the value of the 2 images doesn't actually change anything that gets returned to the initial method invoker. Also, your else statement is useless because whether or not your if statement is true, it will then return secondImage. You need to do something like this:
if ([firstImage isEqual:secondImage]){
secondImage = [self getSecondImage];
}
return secondImage;
This way, you update the value that is returned to the initial invoker.
Long story short: your pseudo-code is correct (a simple loop), but your implementation contains recursion, which is an unnecessary complexity in this case IMO (and seems that you're not quite sure what you're doing).
-(UIImage *)getSecondImage{
MPMediaItemArtwork *firstArtwork = [self.mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *firstImage = [firstArtwork imageWithSize:CGSizeMake(1, 1)];
UIImage *secondImage = nil;
do {
// Choose random song in songsArray + get Artwork
MPMediaItem *myItem = [songTracks objectAtIndex:arc4random() % songsArray.count];
MPMediaItemArtwork *secondArtwork = [myItem valueForProperty:MPMediaItemPropertyArtwork];
// Get image
secondImage = [secondArtwork imageWithSize:CGSizeMake(1, 1)];
// IF the two images are the same, repeat this loop
// IF NOT, return secondImage
} while ([firstImage isEqual:secondImage]);
return secondImage;
}
I need to take the current value of mtype and pass it forward to Mselect so that the image pushed forward is the same as the one rotating in the animation.
here is the code sample
- (void)viewDidLoad
{
//Array to hold Images
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:Mush_Count];
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
//make button
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
Mselect = mtype;
}
}
-(IBAction)Selection:(id)sender{
[self Placement];
}
-(void)Placement{
if (Place1Occ == NO) {
[self Place1];
}
}
-(void)Place1{
if (Place1Occ == NO) {
Place1.image =[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", Mselect]];
Place1Occ = YES;
}
}
The animation loops just fine and the selection of the images works but it's not selecting the image the is currently on the screen it selects the last image on the Array.
Any suggestions?
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
}
That loop is enough to build the array of images - you don't need to set the imageView's animation images until this loop completes and the array is populated.
As it stands, you reset the animation images each loop and set Mselect to type each iteration: this is why you always get the last image index stored in Mselect
This code should be after the for loop:
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
As far as I know, you can't get the current frame of the animation directly - you have to create a timer when you start the animation and have this count the number of frames (by increasing your Mselect value. Then use that to load the current image
Is SelectBT a subclass of UIButton? if so is it the (id)sender coming from your IBAction?
If so then you can just grab the image directly from the SelectBT instance by calling sender.imageView.image
As an aside, objective-C convention is to capitalize ClassNames, but start instances with lowerCase like this ClassName *instanceOfClassName and you're likely to take flack for it around here if you don't adhere to that convention
Update
Could you bump your imageArray into a class variable or a property? It already contains all the fully loaded UIImages, which you can get back out by calling [imageArray objectAtIndex:i]
Then you just have to keep track of which numeric index is facing your user, or which index they tapped and pull the corresponding image.
Feel free to post your whole class if you'd like us to explain more.
I have a simple for loop which changes the images in a set of 9 UIImageViews. However, I have one problem, I can't change the name of the UIImageView in co-ordination of the for loop and so the end result is that the for loop ONLY affects 1 out of 9 UIImageViews I have in my iOS App.
Here is my code:
for (current_photo = 1; current_photo < 10; current_photo++) {
picview_1.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#%i.png", SERVER_URL, current_photo]];
}
You will notice that in my code there is a bit:
picview_1.image
When I replace the "pic view_1" which "picview_current_photo", it comes up with errors.
What am I doing wrong? Please explain.
Thanks for your time :)
Create an array of UIImageViews, and access them in a loop through an index, like this:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, ..., picview_9];
// Arrays are indexed from zero, so I changed the index to run from 0 to 9
for (current_photo = 0; current_photo < 9; current_photo++) {
// Since current_photo is zero-based, I added one to it in stringWithFormat
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#%i.png", SERVER_URL, current_photo+1]];
[[imageViews objectAtIndex:current_photo] setImage:img];
}
You are probably best to set up an array of UIImageViews. Then access the UIImageView that you need from that array and set the image as required.
I made 26 buttons, A-Z and when I click them the image of the button I pressed will display in an image view, when I click A, it displays A, When I click B it will display B etc.
The problem I have is that they won't display next to each other, I have 6 image views and I want the first letter to fill first box second letter second box etc.
I just don't know how to do this, I've been trying a lot of ways now and i'm pretty sure i'll need to use arrays just can't get the code right.
This is a picture of where I am now I'm already past number 1 so it's like number 2 except for showing next each other ofc.
hope someone can tell me here is the picture:
http://img213.imageshack.us/img213/4500/questionow.png
thanks
This is what I got now .h
IBOutlet UIImageView *imageview1;
IBOutlet UIImageView *imageview2;
IBOutlet UIImageView *imageview3;
IBOutlet UIImageView *imageview4;
IBOutlet UIImageView *imageview5;
IBOutlet UIImageView *imageview6;
}
-(IBAction)showA;
-(IBAction)showB;
-(IBAction)showC;
-(IBAction)showD;
-(IBAction)showE;
-(IBAction)showF;
-(IBAction)showG;
-(IBAction)showH;
-(IBAction)showI;
-(IBAction)showJ;
-(IBAction)showK;
-(IBAction)showL;
-(IBAction)showM;
-(IBAction)showN;
-(IBAction)showO;
-(IBAction)showP;
-(IBAction)showQ;
-(IBAction)showR;
-(IBAction)showS;
-(IBAction)showT;
-(IBAction)showU;
-(IBAction)showV;
-(IBAction)showW;
-(IBAction)showX;
-(IBAction)showY;
-(IBAction)showZ;
.m
-(IBAction)showA {
UIImage *img = [UIImage imageNamed:#"A.png"];
[imageview1 setImage:img];
}
-(IBAction)showB {
UIImage *img = [UIImage imageNamed:#"B.png"];
[imageview1 setImage:img];
}
Etc.
Hope someone can tell me how to make this or has some sample code for me because I don't quite get the arrays even after seeing a lot of tutorials on them.
thanks Don
You could always do:
- (void)setNextImage:(UIImage *)image {
if (imageview1.image == nil) {
[imageView1 setImage:image]
} else if (imageView2.image == nil){
[imageView2 setImage:image]
} // ... Through all 6 images
}
and then in each of your button actions just call:
[self setNextImage:img];
instead of setting it directly.
Using an array is not hard. Just declare either a standard C array of UIImageView* or an NSArray, inited like: [NSArray arrayWithObjects:imageView1, imageView2 ... imageView6, nil].
Then have a position counter pos declared at the instance level, and you simply do:
[imageViewArray objectAtIndex:pos].image = imageParm; pos++;
(Or, for the C array, imageViewArray[pos].image = imageParm; pos++;.)