NSString to parameter of type UIImage - iphone

Hi I came along this problem can someone solve this for me?
unichar aChar7 = [textview.text characterAtIndex:0];
if (aChar7 == 'A' ){
[imageview7 setImage: #"BLUEBOXA.png"];
It says
Incompatible pointer types sending 'NSString *' to parameter of type 'UIImage *'
thanks

the UIImageView "setImage" takes a UIImage type as the parameter. So you need to make a UIImage instance first. Do this:
myImage = [UIImage imageNamed:#"BLUEBOXA.png"];
[imageview7 setImage: myImage];
Hope that helps!

The parameter to setImage is an UIImage, not NSString. Try this:
[imageview7 setImage:[UIImage imageNamed:#"BLUEBOXA.png"]];

1 - The following statement will look the name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.
UIImage *blueBoxImage = [UIImage imageNamed:#"BLUEBOXA"];
//see my 3rd point, no need to specify png format
2 - if blueBoxImage returns nil; the method could not find the specified image.
Because this method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
3 - On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension.
Now, once you are ready with your photo, you can frame it :). I mean once got that image object, you are ready to set to your UIImageView
imageView7.image = blueBoxImage;

Related

how to show images at cell.imageview.image from NSArray?

Image Array=[NSArray arrayWithObjects:[UIImage imageNamed:#"1 (1).gif"],
[UIImage imageNamed:#"1 (2).gif"],[UIImage imageNamed:#"1 (3).gif"],[UIImage imageNamed:#"1 (4).gif"],[UIImage imageNamed:#"1 (5).gif"],[UIImage imageNamed:#"1 (6).gif"],[UIImage imageNamed:#"1 (7).gif"],nil];
//here is another array of objects showing in table view
alphabetsArray =[[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",nil];
//everything working fine until when i add this code,app crashes with SIGABRT
cell.imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
Actually i need to ask some more things:
Is there any problem if number of images in array isn't equal to number of object in tableview?
Is there any other way to add images if they are huge in number except of uiimage imageNamed:"whatever.jpeg"
What if add a folder and give its address or name and it picks up images by itself..
What is most reasonable method of adding images in project if they are 250 in number(i-e sqlite,coreData,hardcode in project as i did)?
First, there is issue with this line [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]].
The [imageArray objectAtIndex:indexPath.row] is returning UIImage object in your current implementation, and you are trying to get UIImage with name of UIImage object.
You should just return the UIImage object directly to the cell.imageView.image.
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
Second, I am not sure how many rows are there in the table, but the method objectAtIndex can throw exception if the index is more than the number of elements in the array, causing app crash.
1) There would be no problem with this, unless you want to have the same number. It won't throw an error, but your app logic will not work properly.
2) Not that i know of, It's easy to do this though, use an easy naming convention such as image01, That way you can just copy and paste the code and change the ending numbers of the image
3) Im not sure of what you are asking.
4) Typically you would just need to add the images to your project via drag-N'-drop or by picking the files. Core data and sqlite are for the actual app that you are developing, not to store images in Xcode.
As for the main question, You say you want to show the images from an NSArray, if thats the case, just get the index path of the tableview, and look up the corresponding image using objectAtIndex:
Hope that helps a bit :)
Actually i have figured something out , there is an easy way to do.
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif", [TextShownInTableArray objectAtIndex:indexPath.row]]];
i have alphabetics in this array, and i wanted to use different images with each of the alphabet, so just added images in project with same name as alphabets had. it worked fine with this line of code. TextShownInTableArray is your array with text :)
Image Array=[NSArray alloc]initWithObjects:[UIImage imageNamed:#"8.gif"],
[UIImage imageNamed:#"2.gif"],[UIImage imageNamed:#"3.gif"],[UIImage imageNamed:#"6.gif"],[UIImage imageNamed:#"5.gif"],[UIImage imageNamed:#"4.gif"],[UIImage imageNamed:#"7.gif"],nil];
your array alloc and that array 'retain' this array and try this code .thanks
[imageArray retain];
only change in your code this line . and solve your problem
cell.imageView.image=[imageArray objectAtIndex:indexPath.row];
i hope this code useful for You . Check this
cell.imageView.frame = CGRectMake(5, 5, 50, 50);
NSString *imgstr = [[Arr objectAtIndex:indexPath.row] objectForKey:#"thumb"];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgstr]]];

appending string to file name by passing through variable in objective c

NSString *devicetype;
if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)){
devicetype = #"ipad";
}else{
devicetype = #"iphone";
}
I have two images
BACKGROUND_IPAD.PNG
AND
BACKGROUND_IPHONE.PNG
Now I want to use this string like
BackGround = [CCSprite spriteWithFile:[NSString StringWithFormat:#"background_%#.png",DEVICETYPE]];
Is this possible?
If not, then is there a better way?
Yes you an use it, but insread of that you can change the name of the image to the following
Image~ipad.png
Image~iphone.png
Now when you load the image
//on the iPhone and iPad use the same image name
//image named will select the image automatically
UIImage *image = [UIImage imageNamed:"Image"];
It will automatically select the image basing on your current device
Yes it works. I have tested and running successfully.

problem while displaying image in imageview

i create a view based project named myclass.
and i add another UIviewcontroller subclass to it, named as webdata.
In the webdataviewcontroller.m i get image from the webserver.
I need to display this image on imageview in myclassViewController.m from webdataViewController.m
for this my code in webdataViewController.m is
docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
imagepath = [NSString stringWithFormat:#"%#/image.png",docDir];
logopath = [NSString stringWithFormat:#"%#/logo.png",docDir];
myclassViewController *obj = [[myclassViewController alloc]init];
obj.homeimage.image = [UIImage imageWithContentsOfFile:imagepath];
but image is not displayed,i try to print image in console
NSLog(#"image to display %#",obj.homeimage.image);
NSLog(#"image to display %#",[UIImage imageWithContentsOfFile:imagepath]);
but i get like this
image to display (null)
image to display <UIImage: 0x6f614b0>
why i get null for obj.homeimage.image,i did n't get can any one please help me.
Thank u in adavance.
(Let me add comment if question is not understandable)
Are you sure that obj.homeimage is not nil? If it is nil then obj.homeimage.image = ... will result to setting image property to nil object, that is "nothing".
Why don't you give us the code for myclassViewController ?
Also you have a lot of mistakes in capitalization, etc. It makes it hard to read your code and it's messy.
myClassViewController should be MyClassViewController
webdataViewController should be WebDataViewController
You should camel case variable names
homeimage should be homeImage
imagepath should be imagePath
You said you subclassed myclassViewController, I think by that you meant you added it as a subview. If so when you execute the code:
myclassViewController *obj = [[myclassViewController alloc]init];
you are creating a NEW myclassViewController, not referencing the parent view controller. You need a pointer to the the instance of the myclassViewController that added webdataViewController as a subview, and add the image to it's property. Also I'm guessing here lies your problem, you are creating a new empty instance of myclassViewController therefore you have not initialized your ivars. (Unless you did so in your -init method, but I would doubt that). So you alloc-ed and init-ed a new myclassViewController but none of it's variables are initialized so you are just messaging nil objects probably. Which is why your code isn't crashing yet still not responding. Once again this is a guess, but it probably is the problem.
I can edit with more information if you give the code for myclassViewController.

Dynamically loading images in UIImageView from database in objective c

I have saved my images in database with type BLOB. Now I am extracting these images from database using sql stmt and saving it in a NSData object. see the following code snippet
NSData *img = [[NSData alloc] initWithBytes:sqlite3_column_blob(loadInfostmt, 4) length: sqlite3_column_bytes(loadInfostmt, 4)];
self.birdImage = [UIImage imageWithData:img];
After getting image I tried setting it at image attribute of UIImageView to load it to image view. I used following techniques but nothing seems to work.
1.
self.imgPikr = [[UIImageView alloc] initWithImage:brd.birdImage];
[self.imgPikr setImage:brd.birdImage];
2.
[self.imgPikr = [[UIImageView alloc] init];
self.imgpikr.image = brd.birdImage;
3.
[self.imgPikr = [[UIImageView alloc] init];
[self.imgpikr setImage:brd.birdImage];
Here imgPikr is a UIImageView object and birdImage is object of UIImage class.
every other data is displayed in view correctly except the image.
I dont have any error or warning or runtime exception but still cant load image.
Please tell me the solution. I have been working on it since a week. but couldnt find anything working.
Any answer or experimentation is welcomed.
thanx in advance
Option 1 is the best option, since -[UIImageView initWithImage:] will automatically resize the imageView to be the same size as the passed image (and it is also unnecessary to setImage: if you use this initializer).
However, as #Robot K points out in the comments, you still need to add self.imgPikr as a subview of a view that is visible on the screen.
Additionally, if your imgPikr property is declared as (retain), then you have a memory leak.

Using a variable in a statement to reference the image stored in a UIImageView

UPDATED Scroll down to see the question re-asked more clearly....
If I had the name of a particular UIImageView (IBOutlet) stored in a variable, how can I use it to change the image that is displayed. I tried this, but it does not work.
I'm still new to iphone programming, so any help would be appreciated.
NSString *TmpImage = #"0.png";
NSString *Tst = #"si1_1_2";
TmpImage = #"1.png";
UIImage *sampleimage = [[UIImage imageNamed:TmpImage] retain];
((UIImageView *) (Tst)).image = sampleimage; // This is the line in question
[sampleimage release];
RESTATED:
I have a bunch of images on the screen.... UIImageView *s1, *s2 ,*s3 etc up to *s10
Now suppose I want to update the image each displays to the same image.
Rather than doing
s1.image = sampleimage;
s2.image = sampleimage;
:
s10.image = sampleimage;
How could i write a for loop to go from 1 to 10 and then use
the loop var as part of the line that updates the image.
Something like this.
for ( i = 1; i <- 10; ++i )
s(i).image = sample; // I know that does not work
Basic question is how do I incorporate the variable as part of the statement to access the image? Don't get hung up on my example. The main question is how to use a variable as part of the access to some element/object.
Bottom Line... If I can build the name of a UIImageView into a NSString object, How can I then use that NSString object to manipulate the UIImageView.
Thanks!
Ugh! Your line in question:
((UIImageView *) (Tst)).image = sampleimage;
is casting a string pointer as a UIImageView pointer - you're basically saying that your pointer to a string is actually a pointer to a UIImageView! It will compile (because the compiler will accept your assertion happily) but will of course crash on running.
You need to declare a variable of type UIImageView. This can then hold whichever view you want to set the image of. So your code could look like the following:
NSString *TmpImage = #"0.png";
UIImageView *myImageView;
If (someCondition == YES) {
myImageView = si1_1_2; //Assuming this is the name of your UIImageView
} else {
myImageView = si1_1_3; //etc
}
UIImage *sampleimage = [UIImage imageNamed:TmpImage]; //no need to retain it
myImageView.image = sampleImage;
Hopefully this makes sense!
Edit: I should add, why are you trying to have multiple UIImageViews? Because a UIImageView's image can be changed at any time (and in fact can hold many), would it not be better to have merely one UIImageView and just change the image in it?