I'm creating an app which stores images in the device (save it as coreData) and I have a problem. every time I choose a picture to add the the collectionView, the memory increases in 100Mb or so, and it increases every time. I think I added the ARC , (Edit->refactor->convert to objective-c ARC..) and it doesn't seem to have any problem adding it, but still the memory doesn't release. Here is my code when image is chosen by the user in uiimagepicker:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(chosenImage ,0.2);
if (imageData != nil)
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *urlString = [NSString stringWithFormat:@"myurl.."];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundry = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundry];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
//NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"finish uploading");
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newImage = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];
[newImage setValue:imageData forKey:@"image"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps" message:@"There was an error saving the photo.. try again!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
[photos addObject:imageData];
[self.collectionView reloadData];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}