I have an application in which I am converting UIImage to NSData an further NSData to Base64String using this https://github.com/nicklockwood/Base64 class....but the App crashes When I want to fetch the converted string to the other class.
It gives EXC_BAD_ACCESS error...
Is it possible that NSString can't handle such big strings or is there any other issue?
Here is my code....
image = [UIImage imageNamed:#"earring_hoops_small.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 0.1f);
NSData *data2 = data;
NSLog(#"BaseString=%#",data2);
[Base64 initialize];
strEncoded = [Base64 encode:data];
NSLog(#"BaseString=%#",strEncoded);
and It gives exception while....
myString = strEncoded;
Related
I am creating a greeting card app.I have some existing templates and i also can create new greeting cards by adding cliparts ,textstyles etc.I have saved the image as a uiimage object.I am stuck at the point to save these images for future use.Please help.
To save a UIImage to file do the following
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/img.jpg"];
//Save
NSData* imageData = UIImageJPEGRepresentation(img, 1.0);
[imageData writeToFile:path atomically:YES];
//Load
NSData* imageData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:imageData];
Check out this link.
How to save picture to iPhone photo library?
You can also save the image in iPhone file system by converting image to data and save data to file.
NSData *data = UIImagePNGRepresentation(yourImage);
[data writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile]
i have a NSData object.
I want to convert it to a string, pass to a function and then reconvert back to NSData object.
But How?
I tried this method, but NSData value it's different from the original!
here my code:
// a generic class
APClass *c = [[APClass alloc] init];
c.aNumber = 123;
c.aString = #"my string";
// my data obj
NSMutableData *data = [NSMutableData data];
// archiver to store class in nsdata
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[encoder encodeObject:[NSNumber numberWithInt:c.aNumber] forKey:#"aNum"];
[encoder encodeObject:c.aString forKey:#"aStr"];
[encoder finishEncoding];
[encoder release];
[c release];
NSLog(#"%#", data);
NSString *d = [NSString stringWithFormat:#"%#", data];
// ---
NSString *strFromData = [NSString stringWithFormat:#"%#", d];
NSData *dataNM = [strFromData dataUsingEncoding:NSUTF8StringEncoding];
// decoder to retrieve class from nsdata
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataNM];
int number = [[decoder decodeObjectForKey:#"aNum"] intValue];
NSString *string = [decoder decodeObjectForKey:#"aStr"];
[decoder finishDecoding];
[decoder release];
NSLog(#"[Number: %d] -- [String: %#]", number, string);
How can i convert back to original NSData?
data and dataNM are different in size.
Compiler give back this error:
2012-04-02 16:33:28.269 DataTest[18008:f803] -[__NSCFData
objectForKey:]: unrecognized selector sent to instance 0x6b46c80
2012-04-02 16:33:28.270 DataTest[18008:f803] * Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFData objectForKey:]: unrecognized selector sent to instance
0x6b46c80'
thanks.
Solved.
Using dataUsingEncoding, value of NSData it's different.
To pass data around methods or apps, etc, i've used base64 convertion.
Encode
NSString *d =
[NSString stringWithFormat:#"appdue://obj=%#",
[APBase64Converter base64forData:data]];
Decode
NSData *data = [APBase64Converter base64DataFromString:urlParams];
APBase64Converter Is a lib that encode/decode easily data and strings.
Working example and APBase64Converter lib can be downloaded from here: http://goo.gl/8YNjS
Thanks all.
I "retain" this post for helps people and me next time!
Use
NSData to NSString:
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
and
NSString to NSData:
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
with encoding
NSUTF8StringEncoding
or
NSMacOSRomanStringEncoding (which is an 8-bit encoding).
Use Base64 class it is available in github
DatabaseResult* result = [self.database executeQuery:#"SELECT * FROM `images` WHERE image_id = ? AND imagetype = ?", self.routeId,self.imagetype];
for(EGODatabaseRow* row in result) {
NSString *strImage=[row stringForColumn:#"image_photo"];
[Base64 initialize];
NSData *data = [Base64 decode:strImage];
UIImage *image=[UIImage imageWithData:data];
}
This is how i convert String to data and then data to image.
I need a method to convert a UIImage in a NSString and then convert the NSString back to a UIImage.
Thanks.
for >= IOS 7
- (NSString *)imageToNSString:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
- (UIImage *)stringToUIImage:(NSString *)string
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:string
options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
Convert it to a binary stream instead (NSData). This will depend on the format of your UIImage. If it's a JPEG/PNG for instance, you do:
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSData *data2 = UIImagePNGRepresentation(image);
UPDATE: Converting the binary data to NSString is a bad idea, that is why we have the class NSData. The OP wants to be able to send it as a data stream and then reconstruct it again; NSString will not be needed for this.
Convert to PNG or JPEG using UIImagePNGRepresentation or UIImageJPEGRepresentation, which will return an NSData, and then convert the NSData to a string (not sure how you want to do that mapping). How about just dealing with the NSData? You can read/write that to a file.
I'm creating some temporary files in the iPad simulator. To test my file creation, I create the file and then read it back. Here's some code to show this:
-(NSString *) writeToTempFile:(UIImage*) image{
NSString *path = [self createTemporaryFile];
NSLog(#"path: %#", path);
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES];
free(data);
return path;
}
-(UIImage *) readTempFile:(NSString *) path{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}
I call these methods one after another, before a final function writes out the UIImage to the photo album.
UIImageWriteToSavedPhotosAlbum(image2, self, nil, nil);
The problem is, this always crashes my app on the third time it is executed. First and second time it successfully does all of this and stores to the album. Third time it crashes to Home. Any ideas?
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES];
free(data);
The NSData returned from UIImageJPEGRepresentation is -autoreleased. There is no need to free() it. And it is wrong to free() any Objective-C objects — send a -release message instead.
Please read through the Memory Management Programming Guide.
I want to add my UIImage directly into the file instead of converting into UIImagePNGRepresentation or UIImageJPGRepresentation(as it takes time) like:-
UIImage *im = [UIImage imageWithCGImage:ref];
[array addObject:im];
NSData *data = [array objectAtIndex:i];
[data writeToFile:path atomically:YES];
But it is showing error.
So there is any way that i can do it.
Thanks in Advance.
your use of the array only obfuscates that you are basically doing:
NSData *data = im;
Which cannot possibly work because im is a UIImage, and not an NSData nor a subclass.
What you want to do is to create a new NSData and initialize it with the content of the image. Since you got a CGImageRef, I suggest using it directly, without using a UIImage in between.
CGDataProviderRef imageDataProvider = CGImageGetDataProvider(ref);
CFDataRef imageData = CGDataProviderCopyData(imageDataProvider);
NSData *data = (NSData*) imageData;
Note that it is OK to cast the CFDataRef to NSData* because CFData is “toll-free bridged” with its Cocoa Foundation counterpart, NSData.
I hope that helps.
(don't forget to release data when done)