UIImage memory change while saved in NSMutableArray - ios

I am saving a UIImage to NSMutableArray. After saving, i am talking the object from array and not getting previous memory.
[self.arrselectedPhotos addObject:[UIImage imageWithData:imageData]];
Currently the size is 191565 (192KB). But when i am taking it from array, the size shows 768786 (750KB+).
UIImage *img=[self.arrselectedPhotos lastObject];
NSLog(#"image size: %lu", (unsigned long)[UIImageJPEGRepresentation(img , 1.0) length]);
What is the problem ?

After 3 days of random R&D, i find out the issue of increasing memory size. I was saving the NSData into array after converting it another image. This is the reason. If i save the NSData directly then everything is ok.
This line is wrong:
[self.arrselectedPhotos addObject:[UIImage imageWithData:imageData]];
And the correct line is:
[self.arrselectedPhotos addObject:imageData];
So i can save it furthur easily.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.arrselectedPhotos];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:self.course_name];
[[NSUserDefaults standardUserDefaults] synchronize];

Related

Memory Increase while Retriving data from database for loading image

I am working on application in which I store data of image into database like
NSString *query = [NSString stringWithFormat:#"INSERT INTO Friends_user (f_id,f_name,f_image,f_mobile) VALUES('%ld','%#','%#','%#')",(long)id_, self.txt_name.text,[UIImagePNGRepresentation(self.img_userprofile.image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength], self.txt_mobile_no.text ];
It stores Successfully but when I retrieve that data to display image, Memory Increases with every image. Here is my code to retrieve data
NSData *data = [[NSData alloc]initWithBase64EncodedString:[[arr_friendList objectAtIndex:indexPath.row] objectAtIndex:2] options:NSDataBase64DecodingIgnoreUnknownCharacters];
img_friend_profile.image= [UIImage imageWithData:data];
[theAppDelegate.dbManager executeQuery:query];
My Datatype in database is BLOB.
Memory increases Upto 10-20 MB per image. Please Help me with it
Thank you

How to store JSON response in string and then store that string locally ios objective c

[enter image description here][1]I get three values from JSON. How do I store those three values in NSString and then store those strings locally i.e on the device?
I tried NSUserDefaults but I'm unable to do it. I've attached the code snippet.
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:responseObject
options:0 // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! JSONData)
{
NSLog(#"Got an error: %#", error);
} else {
//_branchId = [responseObject valueForKey:#"branchId"];
_branchName = [responseObject valueForKey:#"branchName"];
_branchUri = [responseObject valueForKey:#"branchUri"];
[[NSUserDefaults standardUserDefaults] setObject:_branchId forKey:[responseObject valueForKey:_branchId]];
[[NSUserDefaults standardUserDefaults] setObject:_branchName forKey:#"branchName"];
[[NSUserDefaults standardUserDefaults] setObject:_branchUriStore forKey:#"_branchUri"]; // Here the setobject value remains nil only
[[NSUserDefaults standardUserDefaults] synchronize];
PS: I want to use that stored value in my commonutility also. How to do that as well?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"(__fetchBranchUri)/%#",url]]];
In the else block replace the contents with
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchId"] forKey:#"branchId"];
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchName"] forKey:#"branchName"];
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchUri"] forKey:#"_branchUri"];
[[NSUserDefaults standardUserDefaults] synchronize];
Edit: Explanation
In your code above the object _branchUriStore is not defined, and may be part of your problem (a simple typo?). Below, I have updated and condensed your code to remove another problem with your usage of branchId and so that you might recognize from the style that much of what you were trying to do is fine.
Also note that #rmaddie has provided additional refinements below in the comments.
Okay here is what you should do.
1- convert your JSONData no NSData
2-Save your NSData in the NSUserDefaults/
Hope this helps!

Unable to save data in NSUserDefaults with crash with Signal SIGABRT

I am trying to save NSData in NSUserDefaults in a loop, but I'm constantly getting an error.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *index = [NSString stringWithFormat:#"alltweetsoftrend%d", i];
[defaults setObject:tweetData forKey:index];
[defaults synchronize];
Where i is index of the loop, it means key is not null in any case. I have also checked my NSData(tweetData) and this also is not null. I am just getting this error in saving.
There's nothing inherently wrong with your code. There's a slight chance the NSData is too large to store, but I wouldn't really expect a SIGABRT in that case. The most likely issue is that the NSData was created with [NSData dataWithBytesNoCopy:length:] and the buffer that the NSData pointed to was deallocated. There could be other issues that cause the problem as well, such as using a shared NSData global variable between threads, incorrect typecasting from CFDataRefs, etc, etc.
Try writing a different NSData and see if it works. For example:
NSData *tweetData = [NSData dataWithBytes:"Hello" length:6];. I bet that works, so the problem has to do with how your tweetData was created and sent into this code.

Saving NSMutableDictionary containing UIButtons in the NSUserDefults

I am trying to save a NSMutableDictionary containing UIButtons in the NSUserDefults in this way:
[UserFolderDictionary setObject:FolderButton forKey:[NSString stringWithFormat:#"%ld", (long)numberFinal]];
[[NSUserDefaults standardUserDefaults] setObject:UserFolderDictionary forKey:#"UserFolderDictionarySaved"];
The app crashes with the error 'attempt to insert non-property list object'. To re-catch it I tried this:
NSMutableDictionary* UserFolderDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:#"UserFolderDictionarySaved"] mutableCopy];
FolderButton is a normal UIButton with a tag and a background image.
You can't save buttons in NSUserDefaults, and furthermore you SHOULDN'T save buttons in NSUserDefaults.
Buttons are view objects, not model objects. You should save state information from your buttons, and then use that information to recreate your button.
You may try this.
`
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[myDictionary setObject:button forKey:#"myBtn"];
//save nsdata
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
[userDefaults setObject:data forKey: #"SavedBtn"];
[userDefaults synchronize];
//now retrieve it
NSData *data1 = [userDefaults objectForKey:#"SavedBtn"];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
`
Hope that Helps!

Saving an Mutable array of images and loading them

I have part of my app that will take a photo and the person can elect to save it i then save the image to an array but what ways can I save it to the phone WITHOUT it being put in the photo library. I tried
UIImage *image = imageView1.image;
[array addObject: image];
NSUserDefaults *default = [NSUserDefault standardDefaults];
[defaults setObject:image withKey:#"saved image"]; //not exact code just showing the method
I use to save the array of images
[defaults synchronize];
then i also use UserDefaults to load array but it does not work. I am wondering if there is a different way to save images without saving them to the photo library.
In principle, you could save the images in NSUserDefaults using
[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image)
forKey:key];
But keep in mind that NSUserDefaults is meant for storage of preferences, not images. You better save the images in the documents folder and store the path in NSUserDefaults (as suggested by Wain).
You cannot save images the way you want. In your case, array contains just pointers to the images, not images itself. So, one way around is, as #Wain suggested, writing image to the disk, and add path of the saved image to the array, and then saving that array to the NSUserDefaults. You can save image to the disk by converting it to the NSData, and writing it in Documents or tmp folders of application sandbox. In order to convert image to the NSData, you could use UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality) method, and in order to write data to the disk, use writeToFile:atomically method of NSData.
Good Luck!
You can save them into the Document directory:
NSData *imageData = UIImagePNGRepresentation(imageView1.image);
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/imageName.png"];
[imageData writeToFile:imagePath atomically:YES];
And save their URLs in NSUserDefaults:
NSUserDefaults *default = [NSUserDefault standardDefaults];
[defaults setObject:imagePath withKey:#"saved image "];
[defaults synchronize];
And simply retrieve image:
UIImage *customImage = [UIImage imageWithContentsOfFile:imagePath];

Resources