I am trying to convert UIImage into base64 string Objective c - objective-c

I tried the solution given here using:
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
The encoded string I am getting is different from the one I can get via uploading an image on web tool like.
Simply my string is different from web and is not able to decode on the web for an image.
my other implementation is
-(NSString*)base64StringForImage{
UIImage *originalImage =_image_PreviewImage.image;
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawInRect:CGRectMake(0, 0, 1000, 1000)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData=UIImagePNGRepresentation(newImage);
NSString *base64String = [imgData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
return base64String;
}
originalImage and newImage both have the images. testing out reducing the image sizes
the option with
NSDataBase64EncodingEndLineWithLineFeed
given encoded string with half image like the below

Related

Converting a Base64 String into a UIImage

I have looked at similar answers to this type of question but am still falling short of converting a Base64 encoded string into a UIImage correctly. I have used http://www.freeformatter.com/base64-encoder.html to test my string and can successfully get an image back as a response.
However, when I use initWithBase64EncodedString, I get a nil response. Below is the code I am using and an example of the Base64 string that I used to test with. What am I missing here?
Code:
imageData = [[NSData alloc] initWithBase64EncodedString:checkBytesString options:0];
checkImage = [UIImage imageWithData:imageData];
String (It is rather large so I am sharing it via OneDrive):
https://onedrive.live.com/redir?resid=60AA391B8FEA9C36!107&authkey=!AFK_y5UHOFsdYsKZI&ithint=file%2c.rtf
Answer was to use an external library, NSData+Base64. It implements method dataFromBase64String that returned imageData properly so it could be converted into an image.
https://github.com/l4u/NSData-Base64
imageData = [NSData dataFromBase64String:frontCheckBytesString];
checkImage = [UIImage imageWithData:imageData];
Simple in iOS 7 onwards
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
UIImage *zeroImage = [[UIImage alloc]init];
if(![strb64Image isEqualToString:#""] && strb64Image)
{
zeroImage = [self decodeBase64ToImage:strB64Image];
}

CGImageRef from NSData is null, but UIImage is not?

I'm trying to move some image loading into the background. Currently i'm loading UIImages in the background but from what I have read, this is not the suggested way to go about doing it, instead I should load the CGImageRef in the background, then load the UIImage from it in the main thread.
The problem is that when I try to create a CGImageRef, its coming back as null. Sample code:
NSData * imageData = [NSData dataWithContentsOfFile: coverPath];
if(nil != imageData)
{
UIImage * uiImage = [UIImage imageWithData: imageData];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef) imageData);
CGImageRef imageRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
NSLog(#"Test: %p, %p", imageRef, uiImage);
CGDataProviderRelease(provider);
}
Which logs out Test: 0x0, 0x1c566bb0. Meaning the imageRef is null but the uiImage is not. Any ideas what i'm doing wrong here? It seems as if this should be quite simple?
CGImageCreateWithPNGDataProvider()
returns nil if the provided data is not in PNG format (for example JPEG or TIFF).
[UIImage imageWithData: imageData]
returns an image for all supported image file formats (PNG, JPEG, TIFF etc.)
This explains why the first function can fail while the second succeeds.

Cache Image From URL working, but returns blank image?

I have two methods, first checks if I've already downloaded the image, and if not retrieves the image from a URL and caches it to my docs directory in my app. If it has been, it simply retrieves it, and if I have a internet connection, will re-download it. Here are the two methods:
- (UIImage *) getImageFromUserIMagesFolderInDocsWithName:(NSString *)nameOfFile
{
UIImage *image = [UIImage imageNamed:nameOfFile];
if (!image) // image doesn't exist in bundle...
{
// Get Image
NSString *cleanNameOfFile = [[[nameOfFile stringByReplacingOccurrencesOfString:#"." withString:#""]
stringByReplacingOccurrencesOfString:#":" withString:#""]
stringByReplacingOccurrencesOfString:#"/" withString:#""];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"Documents/%#.png", cleanNameOfFile]];
image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
if (!image)
{
// image isn't cached
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:nameOfFile]]];
[self saveImageToUserImagesFolderInDocsWithName:cleanNameOfFile andImage:image];
}
else
{
// if we have a internet connection, update the cached image
/*if (isConnectedToInternet) {
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:nameOfFile]]];
[self saveImageToUserImagesFolderInDocsWithName:cleanNameOfFile andImage:image];
}*/
// otherwise just return it
}
}
return image;
}
Here's to save the image
- (void) saveImageToUserImagesFolderInDocsWithName:(NSString *)nameOfFile andImage:(UIImage *)image
{
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"Documents/%#.png", nameOfFile]];
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
NSLog(#"directory: %#", [[UIImage alloc] initWithContentsOfFile:pngPath]);
}
The image has already been successfully downloaded and cached to my documents directory (I know because I can see it in the File system). And it successfully re loads the image the first time I call this method, but once I go to another view, and re-call this method when I come back to the same view, it's blank. Yet, the URL is correct. What's wrong here?
1) You should not be writing into a hardcoded path as you do (ie "Documents/xxx"), but rather ask for the Application Support directory, use it, and also mark files so that they don't get uploaded to iCloud (unless you want that). See this link on the specifics. Create a subfolder in it and mark it as not for iCloud backup.
2) Try changing:
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:nameOfFile]]];
to
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];
Maybe, you should do:
image = [UIImage imageWithContentsOfFile: nameOfFile];

How to compose an image from GUI elements on iOS?

I need to form an image by composing some visual elements and save it on disk. The question is: how to "screenshot" a certain area of a view? Possibly a view that is not visible, so the procedure can be executed unnoticed?
This code snippet shows how to render a view to a UIImage:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
And this snippet shows how to save a UIImage as a JPEG or a PNG:
NSString *pngPath = [NSHomeDirectory()
stringByAppendingPathComponent:#"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory()
stringByAppendingPathComponent:#"Documents/Test.jpg"];
[UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES];
[UIImagePNGRepresentation(viewImage) writeToFile:pngPath atomically:YES];

Get image from Webpage

What I am trying to do is to get image from that url
www.floraphotographs.com/showrandomiphonestuff.php?color=red&session=5345
The site displays a random image everytime , I need to be able to get the image in a NSImage object and display on the screen
Grab the data like so:
NSURL * url = [NSURL URLWithString:#"http://www.flora...."];
NSData * data = [NSData dataWithContentsOfURL:url];
Then stick it in a UIImage:
UIImage * image = [UIImage imageWithData:data];
Then put it in a UIImageView:
imageView.image = image;