Transparent UIImage in iphone app's Document folder - iphone

As i am saving a image which has transparent part but while saving it in Document folder, the transparent part changed to white.
Below is the code ....
NSData *dataPhoto = UIImageJPEGRepresentation(imageBigPhoto, 1.0);
//NSData *dataPhoto = UIImagePNGRepresentation(imageBigPhoto);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathWithFoldername=[documentsDirectory stringByAppendingFormat:#"/%#",[[NSUserDefaults standardUserDefaults]valueForKey:#"PhotoFolderName"]];
//NSLog(#"%#",fullPathWithFoldername);
NSString *fullPath = [fullPathWithFoldername stringByAppendingPathComponent:name];
[dataPhoto writeToFile:fullPath atomically:YES];

JPEG images don't support transparency. PNG images do.

I think you have commented your PNG Representation of Image, JPEG Representation does not save transparency. Just Enable you commented code and try.

Related

Xcode-Saving the image from the photo library for further use

I am trying to make a simple application using XCode 4.5 which would allow the user to chose any particular image via accessing his photo library, then submit that image for recognition with the help of the Tesseract library.
The problem is that I do not know how to further save the selected user picture, In other words, I can provide the user with an option for going in the picture library and let him chose a picture, but I do not how to then save that selected picture so that it can be further processed.
I hope i made it clear, Appreciate your help.
Try this,
Save image:
NSString *imgName=[#"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:#"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
Retrive image:
NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:#"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:#"%#/%#",documentsDir,imageName];
[imageview setImage:[UIImage imageWithContentsOfFile:filePath]];
What you need to do is save the image data after selection inside the following delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { You can get the image via the key UIImagePickerControllerOriginalImage in the info dictionary and possibly assign it to an imageview or save it as mentioned by #Kalpesh. Check out apple's documentation for more info. Also check out this good tutorial that shows how to pick photos from the photo album or the camera.
I am still not clear about what exactly you want.
You can always Save Image in or Retrieve Image from Photo Library.

what is the best way to store bulk of images and MP3 song iOS Sdk

i am doing one iphone app, for that i have to store bulk of MP3 song and images.
can ay one tell me what is the best to store those in terms of performance.
Store the image and songs in the application directory. This is best and easy way to handle. Try the following code. it will be help you.
//Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return [data writeToFile:appFile atomically:YES];
}
//Image/songs - Retrieve from Application Directory
+(NSData *)readFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
return myData;
}
return nil;
}
store the MP3 song and images into resources folder of your app project and give the refrences in the sqlite database(because saving large files in sqlite database is not a good practice)
I would suggest you to store all items of large size on the device disk, i.e, Documents directory and store their physical path in core data or sqlite or at least in a plist file so that you can retrieve them as per your convenience.

Images not loading from sub directories of document directory

HI,
I am loading images from sub directory of document directory. Here is the code.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
cell.imageView.image = [UIImage imageWithContentsOfFile:[documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"images/category/%#", element.image]]];
My images resides at DocumentDir/images/category/img1.png, DocumentDir/images/category/img2.png, DocumentDir/images/category/img3.png .....
However when images are at DocumentDir it loads fine. But when it is in sub directories like above it does not load. Am I doing wrong here?
Thank you
Solved the issue. There was a new line character at the end of the "element.image" variable.
NSString *docPath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"images/category/%#", element.image]];
cell.imageView.image = [UIImage imageWithContentsOfFile: docPath];
use this, One more thing image file should contain the .png or .jpg. It is required when you use the imageWithContentsOfFile method.

Show image from documents folder

I retrieve an image I have saved at the documents directory like this:
//set background image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"/myFolder/back.png"];
backgroundImage.image =[UIImage imageWithContentsOfFile:imagePath];
But it is not shown :(
What am I doing wrong?
PD:Yes, it has been saved with the same path and filename (no upper/lower case differences) and yes, I tried opening the image at that path and it works.
Thanks in advance!
It was being done in the wrong place. I was doing it at init instead of viewDidLoad.

Does the iPhone compress images saved within my app's documents directory?

We are caching images downloaded from our server. We get the data from an ASIHTTPRequest callback like this:
#pragma mark ASIHTTPRequest callback
-(void)imageDownloadFinished:(ASIHTTPRequest*)aRequest
{
NSString* fileName = aRequest.url.path.lastPathComponent;
[self imageDidArrive:[aRequest responseData] forFileName:fileName];
}
We write the image data to our local storage like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:#"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:#"/%#", aBaseFilename];
BOOL writeSuccess = [anImageData writeToFile:fileName atomically:NO];
The downloaded images are always the expected size, around 45-85KB.
Later, we read images from our cache like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0] ;
NSString* folder = [[documentsDirectory stringByAppendingPathComponent:#"flook.images"] retain];
NSString* fileName = [folder stringByAppendingFormat:#"/%#", aBaseFilename];
image = [UIImage imageWithContentsOfFile:fileName];
Occasionally, the images returned from this cache read are much smaller because they are much more compressed - around 5-10KB. Has the OS done this to us?
Edit - it turns out that we are downloading the small images, so the issue isn't on the iPhone
If I'm reading your code correctly, you're using the NSData method writeToFile:atomically: to write to the file. That does an exact byte-for-byte write of the contents of the NSData object.
It appears that the NSData object is created directly from the contents of the HTTP response, so the answer is "no", there should not be any compression taking place.
We have the solution. When the phone is running on the 3G network, O2 kindly steps in and applies extra JPG compression to our images, so that they look extra horrible.
See this post on the UK 3G forum.