Memory increase when loading image with encoded data - ios

I am working in project in which I store string of image data to database by using this library https://github.com/bborbe/base64-ios/tree/master/Base64.
So when I get that image field I decode that data and shows image
My code to encode Image data
imageData = UIImageJPEGRepresentation(self.img_user_profile.image, 0.4);;
NSString *strEncodeImg = [Base64 encode:imageData];
My code to decode image data
NSData *str = [Base64 decode:[[arr_user_info valueForKey:#"image"] objectAtIndex:0]];
_img_user.image =[UIImage imageWithData:str];
My problem is When I get data string from database and load image in imageview, Memory increases with every image.
Please help me with it

Instead of declaring the attribute as NSString you can declared that as NSData. Now you can directly store the imageData into core data. When fetching you can use imageData directly to load the image

Related

UIImage encoded to NSData in ObjectiveC and then decoded in Swift

I have an app that was originally created in objective C (lets call this version 1) and I have now converted my app to Swift 2.0 (version 2). One of the main functions of the app is the ability to send images and text from one device to another. Images and text are stored in a NSMutableDictionary and then encoded to NSData and the sent / stored on the Parse backend server. The design of my app also has the ability to email an image from one device to another.
This is working well for both versions of my app – Objective C and Swift. Great !
My problem is when a user sends NSData from version 1 of my app to a device with version 2 (basically an image encoded in objective C and then decoded in Swift) !! Encoded text decodes fine but not the image (saved as objectForKey("data")). See below example. quizData is an array the holds dictionary (keyValue items) that have been sent from another device. This array works with all items except for objectForKey("data"). This object is the encoded image.
var imageData = NSData()
imageData = quizData.objectAtIndex(currentQuestionNumber).objectForKey("data") as! NSData
// the following always prints out lots of info to confirm the imageData has the encoded image
print("imageData.length = \(imageData.length)")
print("imageData.description = \(imageData.description)")
// decoding
photoImageView.image = UIImage(data:imageData)
ok, so the above works when the image was created on another device using Swift. But if the image created and sent from version 1 (objective c) the photoImageView is blank (no errors) yet the imageData is huge (the printout shows that imageDate does hold the users image).
Surley if an NSdata object has the data for a UIImage it should be able to be decoded in ObjC or Swift ?? No problem sending more code if required
Question amended as follows :
Not sure if this really helps but heres objC code for sending a NSData via email (all app data is saved a pList)
// emailArray to be populated with selected data from plist
NSMutableArray *emailArray = [NSMutableArray arrayWithContentsOfFile:path];
MFMailComposeViewController *emailPicker = [[MFMailComposeViewController alloc]init];
emailPicker.mailComposeDelegate =self;
/// NSdata from emailArray
NSData *emailQuizData = [NSKeyedArchiver archivedDataWithRootObject:emailArray];
[emailPicker addAttachmentData:emailQuizData mimeType:#"application/quizApp" fileName:_quizNameLabel.text];
if you use base64 encoding this issue shouldn't arise.
here is the implementation in swift:
import UIKit
func base64StringForImage(image: UIImage) -> String? {
guard let data = UIImagePNGRepresentation(image) else { return nil }
return data.base64EncodedStringWithOptions([])
}
func imageFromBase64String(string: String) -> UIImage? {
guard let data = NSData(base64EncodedString: string, options: []) else { return nil }
return UIImage(data: data)
}
here is the implementation in objc:
#import <UIKit/UIKit.h>
NS_INLINE NSString * base64StringForImage_objc(UIImage *image) {
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData base64EncodedStringWithOptions:0];
}
NS_INLINE UIImage * imageFromBase64String_objc(NSString *string) {
NSData *imageData = [[NSData alloc] initWithBase64EncodedString: string options: 0];
return [[UIImage alloc] initWithData:imageData];
}

Proper way to get the byte array from the JSON

I am trying to get the image from the byte array. I can only get the image if enter the byte array values into the string directly as follows:
NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-1,-40,-1,-32,0,16,74,70,73,70,0,1,0,1,0,96,0,96,0,0,-1,-2,0,31,76,69,65,68,32,84,101,99,104,110,111,108,111,103,105,101,115,32,73,110,99,46,32,86,49,46,48,49,0,-1,-37,0,-124,0,5,5,5,8,5,8,12,7,7,12,12,9,9,9,12,13,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13];
//this way works fine
IF i try to get the byte array into string as follows,then I couldnt get the image:
NSString *logo=[NSString stringWithFormat:#"%#",[[JSON valueForKey:#"request"]valueForKey:#"logo" ]];
NSMutableString *imagen = [[NSMutableString alloc] initWithString:logo];
//this way doesnt work out
I am following this link to get the thing done.this link
Could you please tell me whats the proper way to get byte array from JSON?
This is not executable code, it is a method to use:
You get the JSON in self.receivedData
Convert it into an object with
NSDictionary *jsonObject = NSJSONSerialization JSONObjectWithData:
** Unknown how the image data is encoded in the JSON. If it is Base64 encoded:
Get the Base64image string with
NSString *imageString = jsonObject[#"request"][#"logo"]
Convert the Base64 string into data:
NSData *imageData = [NSData alloc] initWithBase64EncodedString: imageString options:
Get an image with
UIImage *logoImage = [imageData imageWithData]
All in all you have way to much code that accomplished nothing and converting the image data to an NSString is incorrect.
Converting to a string and then back to data accomplishes nothing.
This code seems confused? The link you posted has string encodings which contain negative numbers. You parse these as signed but then assign them to an array of unsigned uint8_t using bytes[i] = (uint8_t)byte;.
I think you need to post the string encoding format and an example?

Base64 encoded string wont decode (iOS)

I am base64 encoding some data using the following line:
NSString *theData = [serialized base64EncodedStringWithOptions:kNilOptions];
This works correctly and I then pass this string to my web server which stores it in the database.
Later, I am then retrieving this base64 encoded string back from the web server which also works correctly (I have compared both the original before upload and the after download strings and they are the same).
However, when I try to decode this string using:
NSString *theString = [imageDict objectForKey:#"image"];
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:theString
options:kNilOptions];
it just gives me a null value for imageData.
If I output theString it is the correct base64 encoded string I uploaded.
Any ideas why it won't decode?
Thanks in advance.
I used NSDataAdditions.h < see http://code.ohloh.net/file?fid=28qaXmo6xH1Z4clfmn9_wJqDqNI&cid=xVjpNPxNo_A&s=&fp=308694&mp=&projSelected=true#L0 and for .m http://code.ohloh.net/file?fid=tXQCCVHemN1iAx6ZQSy1VkBACXA&cid=xVjpNPxNo_A&s=&fp=308694&mp&projSelected=true#L0 >
NSData *imageData = [NSData dataWithBase64EncodedString:theString];

How to convert json with data type of Byte to UIImage

I am seeking for a help on how to convert a json value with data type of byte into an image. I don't have any idea of this.
Try this, I am assuming you have base-64 encoded form of image
NSData *imageData = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *image = [UIImage imageWithData: imageData];
There is no data type of "byte" in JSON. The data types are array, dictionary, string, number, bool and null. You might be getting a string with a base-64 encoded image. (You might get an array of numbers if the website developer is mad enough) Decode it to get an NSData object, pass that to the right UIImage initialiser.

convert UIImage to NSString - without using encoding & decoding

I want to convert the UIImage to NSString without using any encoding and decoding methods. Following code is used.. please guide me.. Here i have used "encoding:NSASCIIStringEncoding". But I don't want to use. I want to use the binary directly. is it possible ?
UIImage *image = [[UIImage alloc]initWithData:imageName];
NSData *imageDataString = UIImagePNGRepresentation(image);
NSString *content = [[NSString alloc]initWithBytes:[imageDataString bytes] length:[imageDataString length] encoding:NSASCIIStringEncoding];
Thanks in Advance.
If you would like to save it as a binary file, why don't you just use NSDatas writeToURL:atomically: method? If you don't want to save a file - just continue working with NSData. It is an objective-c wrapper around your raw binary data.

Resources