UIImage couldn't load some png picture - cocoa-touch

I Use the following code to load png image:
UIImage *imageBack1 = [UIImage imageNamed:#"Bar1.png"];
UIImage *imageBack2 = [UIImage imageNamed:#"Bar2.png"];
imageBack1 work right when imageBack2's value is nil, Bar1.png and Bar2.png are located at the same place,but why Bar2.png couldn't be load?

In fact , the problem come from the png file,but not cocoa. Iphoto couldn't recognize it.

Related

UIImage imageNamed does not display the image with xcode 4.4

I've been stuck with adding a UIImage to my iPhone application, to add an image i usually used the name of the image and put it like that :
UIImage* myImage1 = [UIImage imageNamed:#"myImage.png"];
But in my application i don't know the name of the image when i'm writting the code, i've got the image name with a webservice and i put it in a NSString.
So i have a string m_image1, and i want to display the image in a UIImage like that :
UIImage* myImage1 = [UIImage imageNamed:m_image1];
But nothing appears on the screen, when i use NSLog to show if m_image1 has the right string it works, and when i paste what is print in the log in the imageNamed it works.
I've already tried using the path of my application, but nothing do it ! So i don't know what to try anymore !
That's my all block of code :
NSLog(#"m_image1 : %#",m_image1);
UIImage* myImage1 = [UIImage imageNamed:m_image1];
m_result1ImageView = [[UIImageView alloc] initWithImage:myImage1];
m_result1ImageView.frame = CGRectMake(0.f, 0.f, 163.f, 260.f);
[m_ScrollImages addSubview:m_result1ImageView];
Everything work just fine, when i put :
UIImage* myImage1 = [UIImage imageNamed:#"myImage.png"];
instead of :
UIImage* myImage1 = [UIImage imageNamed:m_image1];
it display the image !
I think your image is not in the resources!
Check your image: Go to the Project Navigator and click on blue icon which is your project. Now in Targets click on your target and go to Build phases tab and check Copy bundle resources.
Your image file must be in the list, if it is not add it and check the program again!
I hope it be useful for you!
try this code ,
m_result1ImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: m_image1]]];
I think you have not copied exactly the images in your application bundle that is being not displayed .
Try & check removing bunch of images and add those again in your project.

Retina display for an image from URL

I have some images I need to get from the web. Just using data from a URL.
They need to show correctly on Retina Display.
When I get the images from the web, they still look pixelated. I need to set the images' scale to retina display (2.0), but I must be missing something.
Here's what I did so far.
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:#"http://www.msdomains.com/tmp/test.png"];
CGRect labelFrame = CGRectMake(0,0,64,64);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];
imageView.contentScaleFactor = [UIScreen mainScreen].scale;
[imageView setImage:img];
[self addSubview:imageView];
[imageView release];
Try adding ##2x.png at the end of your URL. That wont change the URL, but the image will be recognized as a retina #2x image. It worked for me, but I used this method with SDWebImage.
e.g. using http://www.msdomains.com/tmp/test.png##2x.png.
Your code should work pretty much as-is. I don't know what the original dimensions of your image were, but I'd guess they were 64x64 px. In order to scale down correctly, the original image would need to be 128x128 px.
As a test, the following code correctly displayed my photo in Retina resolution on the Simulator, and on my iPhone 4:
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.seenobjects.org/images/mediumlarge/2006-08-19-native-lilac.jpg"]]];
CGRect labelFrame = CGRectMake(0, 0, 375, 249.5);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];
[imageView setImage:img];
[self.view addSubview:imageView];
Note that the UIImageView is 375x249.5 points, which is half of the original (pixel) dimensions of the photo. Also, setting the contentScaleFactor didn't seem to be necessary.
(As an aside, I can't see that specifying #2x on the URL will help, in this case, as the call to dataWithContentsOfURL: will return an opaque blob of data, with no trace of the filename left. It's that opaque data that's then passed to imageWithData: to load the image.)
when you directly assign the image URL to imageView, it will not take it as retina.
imageView.imageURL = [NSURL URLWithString:#"http://example.com/image.png"];
will not give you a retina image.
So, inspite your image is 200x200 but if your imageView is 100x100 then it will take 100x100 from the downloaded image and show pixelated image on retina devices.
Solution would be to use the image property of imageView instead of imageURL.
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://example.com/image.png"]]];
This will assign 200x200 image to the imageView of 100x100 and hence the image will not be pixelated.
For retina display, add the same image with the resolution which is exactly the double of the original image. dont forget to add "#2x"at the end of this image name... e.g. "image_header.png" is an image 320x100 then another image with name "image_header#2x.png" (dimension 640x200) will be selected for the retina display automatically by the OS...
hope it helps

How to take graphics drawn in the UIView and save as a jpg or something else on the iPhone to pull up in the "Photos"?

How to take graphics drawn in the UIView and save as a jpg or something on the iPhone to pull up in the "Photos" and or attach to an email?
thanks
It's fairly straightforward to get a UIImage:
UIImage* image = nil;
UIGraphicsBeginImageContext(_myView.frame.size);
{
[_myView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
Then save to the photos library with this:
UIImageWriteToSavedPhotosAlbum(_myView, ,

UIImage UIImageJPEGRepresentation safe to UIImage

I need to perform UIImageJPEGRepresentation method and after that save it NOT to file, I need to save it to UIImage. Help please..
you can use this:
NSData *imageData = UIImageJPEGRepresentation(uiimageObject,1.0);
UIImage *imageObj = [UIImage imageWithData:imageData];
I have no idea why you are wanting to go from a UIImage to jpeg data and then right back to a UIImage, but [UIImage imageWithData:jpegdata] should do it.
Eh? UIImageJPEGRepresentation converts an image to JPEG data...

Is there any other way than #2x to let iOS know that the graphic is Retina Display ready?

I have a web service producing two versions of graphics; one for Normal Display and another for Retina Display.
Unfortunately I can't add the #2x to the filename since I don't have access to that code.
Is there any way to let the iPhone know that what's loading from the web is a #2x graphic?
Yes there is ... when you load an image resource into an UIImage you can set the scale of that image yourself, ie. tell the iOS whether your image is #2x or not.
This is the code to load the #2x images (in the example from a file, but you can put whatever you want):
[[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path]] CGImage] scale:2.0 orientation:UIImageOrientationUp];
This is the code to load low res images:
[[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path]] CGImage] scale:1.0 orientation:UIImageOrientationUp];
Cheers, Marin