iOS-optimized png with transparency - ios

I'm using this excellent OptimizedPNG for downloaded (not Xcode bundled) images, but it appears to turn transparent pixels black. Is there a similar utility (optimized using CgBI format) that works for PNGs that include transparency?
example usage as requested:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:IMAGE_URL]]];
NSData *data = [image optimizedData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"optimized-image.png"];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];

Easy answer: Forget "optimized PNGs".
http://imageoptim.com/tweetbot.html
http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/
I am using PNGOUT to compress the images as good as possible and I can
verify that there is no difference in speed.

try setting the UIImageView's backgroundColor property to [UIColor clearColor];

Related

UIImages not recovered Once Device is Restarted

In iOS 8, I am writing UIImages on the device using the code
NSString *documentsDirectory = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *pathString = [NSString stringWithFormat:#"%#/%#%ld",documentsDirectory, #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]];
NSData *imageData = UIImageJPEGRepresentation(image, 40);////I have replaced it with UIImagePNGRepresentation as well
[imageData writeToFile: pathString atomically:YES];
The write to file is successful.
If I try to recover the image using the function,
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
NSString *pathString =[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%ld", #"ProfileImage", (long)[[NSUserDefaults standardUserDefaults] integerForKey:#"PatientsId"]]] ;
UIImage *image = [UIImage imageWithContentsOfFile: pathString];
The image is recovered.
However, if I restart the device, the images are not there anymore. The function [UIImage imageWithContentsOfFile: pathString] returns nil and if I use NSData *myData = [[NSData alloc] initWithContentsOfFile: pathString];, the myData is nil as well.
This procedure was working perfectly fine prior to iOS 8, however, it isn't working properly anymore. Can someone please guide me what is wrong with this?

xcode5: how can I save and load an image?

I'm developing an app, where I can apply filters on an image. I want to create an undo button which resets the original image in the imageView.
The solution is that I just save the original image in it's own UIImage object before I apply any filters to it. This way I can just go back to that in my undo method.
Does somebody know how I can do this?
Take a look at the docs here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html
You can do what you want with UIImagePNGRepresentation().
Then to re-read: imageWithContentsOfFile:
Get Image from disk . Suppose image stored in document directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourImgPath = [documentsDirectory stringByAppendingPathComponent:#"1.png"];
UIImage *originalImage = [UIImage imageWithContentsOfFile:yourImgPath];
Create tempImage from originalImage
UIImage *tempImage = originalImage;
Appy filters on tempImage
EDIT : How to save then below is method to save in document directory.
- (BOOL)saveImageInDocDir:(UIImage *)image withImagename:(NSString *)strImgName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strImgName];
NSData *imageData = UIImagePNGRepresentation(image);
BOOL isSaved = [imageData writeToFile:savedImagePath atomically:YES];
return isSaved;
}
You can also use NSTempraryDirectory to save images as its safe from cloud backup

UIImagePickerController corrupts image

I'm working on an app and I need to get bytes from an UIImage.
For the moment, I use UIImagePickerController, and get the UIImage like this,
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
and then get data from UIImage using this code,
NSData *data = UIImageJPEGRepresentation(image, 1.0);
However, data appears to get corrupted in this case.
On other hand, I have add the jpeg to app's bundle, and tried to convert the image then to data,
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"mapa" ofType:#"jpeg"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
Surprisingly this works perfectly.
Here are my questions:
How can I get the bytes of image selected from UIImagePickerController correctly?
Is there a way to add photos to the bundle programmatically?
Thanks!
EDIT:
I have some metadata bytes on the original JPEG ("GEO-Information").
If i store the jpeg on the ipad, that metadata disappear, and I need them :S
Use the below code to store image in document directory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
[data writeToFile:savedImagePath atomically:NO];
Here 'data' will be the NSData you have obtained from your image captured.

Can't Reload UIImage from Saved file in iPhone Documents Directory location

I am making an app that will save a file to the iPhones Documents Directory and then when the View Controller is reloaded it will fetch it from that same location and display it. But it is not fetching it and displaying it.
I am using the following method to save the image:
void UIImageWriteToFile(UIImage *image, NSString *fileName)
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *filePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
}
I am trying to recall the image I have saved from the viewDidLoad method with the following code:
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = dirPaths[0];
NSString *fileName = [NSString stringWithFormat:#"/%#.png", self.fullname.text];
NSString *filePath = [documentDirectoryPath stringByAppendingString:fileName];
[self.imageView setImage:[UIImage imageNamed: filePath]];
However, it is not showing my image. Can anyone help me and tell me where I am going wrong please. I am new to iOS Development.
replace this line: [self.imageView setImage:[UIImage imageNamed: filePath]];
with this one :
UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath];
[self.imageView setImage:image];
You can use [UIImage imageNamed:] with images from the Documents folder and have the benefits of the underlying caching mechanism, but the file path is slightly different.
Use the following:
NSString *fileName = [NSString stringWithFormat:#"../Documents/%#.png", self.fullname.text];
UIImage *image = [UIImage imageNamed:fileName];
You need to use method
- (UIImage*)initWithContentsOfFile:(NSString*)path
to init images from Documents or Cache directories.
imageNamed: is used for getting images from application bundle.

Displaying Saved Images in iOS 5

I have an app that I am saving images to the NSDocumentDirectory. I can see that the path is being set correctly but when I try to retrieve them the NSData is null.
Here are some of my snippets..
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myApp"];
//Create unique filename
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString];
path = [path stringByAppendingPathExtension: #"png"];
[imageData writeToFile:path atomically:NO];
[self showPhoto:path];
Then in my showPhoto method
NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];
If I debug pngData it is 0 bytes but my path show what I think is the correct path?
Any way of debugging if the photo is actually on the phone? Im not sure if it is getting saved or not.
You can do something like this and check if the file exists first and use imageWithContentsOfFile: to retrieve it.
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath];
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists == YES)
{
UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath]
}
If the image is still empty then there is something wrong with your image.
If you running app in simulator you can find application files here: /Users/{user}/Library/Application Support/iPhone Simulator/5.0/Applications
If you debugging on iPhone you can use iExplorer to see files on iPhone.

Resources