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

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.

Related

Error when adding an image to an Mail - MFMailComposeViewController

I'm trying to send an Image, which I have added to my Project, using the MFMailComposeViewController.
I tried this in the simulator and it works fine, I can also see the Image in the Textfield, but when I'm trying to send the Mail, I get the following error on console:
Feb 10 16:22:02 markuss-macbook-pro.speedport.ip RMC Konus[1825] : CGImageCreate: invalid image size: 0 x 0.
My code:
//Get the Image
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
//Create the Mail View
MFMailComposeViewController *mailCV = [[MFMailComposeViewController alloc] init];
[mailCV setMailComposeDelegate:self];
[mailCV addAttachmentData:imageData mimeType:#"image/png" fileName:#"exGraphic.png"];
//Show the View Controller
[self presentViewController:mailCV animated:YES completion:nil];
The problems only appears in the simulator.
I check these code on many different devices and it still works without problems. So I would say, it is a problem of the simulator ...
Return Value of UIImagePNGRepresentation (from apple document)-
A data object containing the PNG data, or nil if there was a problem
generating the data. This function may return nil if the image has no
data or if the underlying CGImageRef contains data in an unsupported
bitmap format.
that means check whether your image is supported for converting it into NSData with png format, because you are receiving NULL here.your error CGImageCreate: invalid image size: 0 x 0. says that it is NULL.
check by changing image . also check without using [UIImage imageNamed:#""] because this method has many disadvantageous.
another one thing, you have converted image.png image into NSData & sending another image exGraphic.png as attachment,check it

How to compare image string (name?) and change the image

In IOS (iPhone) - I want to show the user an Image, he should write a text string (like the image name or something), If he writes the right string the image changes to a different image, if he makes a mistake and type the wrong string, nothing happens
take user's entered string into
NSString *imageName = userTxtField.text;
UIImage *image = [UIImage imageNamed:imageName];
if(image != nil){
// change previous image
}
May be you are trying to build an app like "Brands"
Once user types and press submit button compare imagename with user string
if([textField.text isEqualToString:IMGNAME]) {
imageview.image = nextimage;
}
You should use ImageNamed function of UIImage class.
If return value is nil then an image doesn't exist.
For example:
-(UIImage*)getImageNamed:(NSString*)name{
UIImage* Image = [UIImage imageNamed:name];
if (!Image)
NSLog(#"Sorry, there is no image named %#.",name");
return (Image);
}

NSString to parameter of type UIImage

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;

How do I read in & display a local image on UIImageView via SQLite

How may I modify, read in an image path from SQLite DB, & display it via an UIImageView? In other words, this application will work in offline mode & i have the images at hand. How will i go about this then, tgt with sqlite? (retrieve images by storing their image path name # sqlite)
Sample code I have for now:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
//start with index 0
currentStepIndex = 0;
Resolution *resolutionObj = [appDelegate.resolutionArray objectAtIndex:currentStepIndex];
[resolutionDescription setText:resolutionObj.stepDescription];
//checking for null string
//replicate of ifelse #goToNextStep & #goToLastStep (needed for initial load of image)
if (resolutionObj.imageDescription != NULL)
{
//if goes in here, then image description is not null
NSURL *imageURL = [NSURL URLWithString:resolutionObj.imageDescription];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[resolutionImage setImage:[UIImage imageWithData:imageData]];
}
else
{
//if goes in here, then image description is NULL
//show empty image
[resolutionImage setImage:NULL];
}
}
You are missing a lot the way I see it. If your images are coming from a website then you should use a webView to display it. If you are planning to display images in UIImageView, you should first download them onto your device from the url that points to that image and save it in the sandbox. Then you store the path of this downloaded image in your sqlitedb. Now when you want to load this image you just access this image using the path that you have in your sqlitedb and render it. How do you think by just saving the imageName image013.png and not downloading the image itself will render the image for you in UIImageView. Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. For going and finding on the web directly, you need a UIWebView and not an UIImageView
If you are packaging your images as resources with your app binary, you can load them as shown below:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myimagefile" ofType:#"png"]];
[self.testView.imgView setImage:img];
where myimagefile is the name of your file and ofType is the type of image. .png or .jpg or whatever.
If you have anymore doubts. Please come back.

Table image not showing, "Pop an autorelease pool" error

I have a UITableView which uses the following code to display an image in a Table View cell:
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
UIImage *image = [UIImage imageNamed:[[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
if ( image ) {
cell.imageView.image = [image imageScaledToSize:CGSizeMake(50, 50)];
}
It works fine on the iPhone simulator, but when I try it on a real iPhone the iPhone doesn't show. Instead in the console in debugging mode, I get this error:
attempt to pop an unknown autorelease
pool (0x851e00)
Any help would be great, thanks.
Check that image != nil. If it == nil then problem in that [[color types] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] code.
Generated file name must be equal real file name including extension, case and must not contain whitespaces.