I'm using ios 7 and I need to do:
In separete thread I want to create image from network and put it into UIImageView. I need to do this every 200 ms.
My code looks like:
- (void)startPreview:(CGFloat)specialFramesRates;
{
if(isPreview)
return;
[Utils runOnThread:^{
[Log log:#"Start preview"]; //here we have a leak
isPreview = YES;
while(isPreview) {
[self getNewBitmap];
sleep(fpsTime);
if(!isPreview)
break;
if(checkAvabilityCam > 10)
break;
}
[Log log:#"Stoped preview"];
}];
}
- (void)getNewBitmap
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:1];
[request setHTTPMethod:#"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(delegate && response) {
checkAvabilityCam = 0;
//TODO what I should do here?
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
[delegate onShowImage:response]; //here I show image in main thread
image = nil; //With or without doesn't work
return;
}
checkAvabilityCam++;
if(delegate)
[delegate onShowDefaultImage];
}
In this line of code I have a problem:
//TODO what I should do here?
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
[delegate onShowImage:response]; //here I show image in main thread
image = nil; //With or without doesn't work
What can I use instead of "[UIImage imageWithData:]" ? I tried save into file and load but with the same effect. What should I do?
UIImage *image = [UIImage imageWithData:newImage]; //HERE IS LEAK !!!!
You are creating an autoreleased object here. Since you're doing this on a background thread, any autoreleased objects you create won't get released unless your thread has its own autorelease pool.
If you are using ARC, create an autorelease pool with the #autoreleasepool keyword:
#autoreleasepool {
UIImage *image = [UIImage imageWithData:newImage];
// Do stuff with image
}
If you're not using ARC, create an autorelease pool manually:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageWithData:newImage];
// Do stuff with image
[pool release];
Related
I setup a background thread with the Parent/Child model. Essentially the context save is failing.
Here is my setup. In the AppDelegate i've setup the _managedObjectContext with the NSMainQueueConcurrencyType:
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];//[[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
In my data loading class I setup the parent/child mocs here to perform the work on the background thread:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSManagedObjectContext *mainMOC = self.managedObjectContext;
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[moc setParentContext:mainMOC];
[moc setUndoManager:nil];
When the json data has completed I attempt to peform a save operation with the following macro:
#define SAVE_MOC { NSError *error; \
if (![moc save:&error]) { NSLog(#"Sub MOC Error"); } \
[mainMOC performBlock:^{ NSError *e = nil; if (![mainMOC save:&e]) {
NSLog(#"Main MOC Error %#",error.localizedDescription);}}];}
Also when i've completed the data load I jump back on the main thread like this:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"<---- complete CS sub moc! ---->");
//this fires ok
});
So, from my SAVE_MOC macro i just get a simple error:
Main MOC Error (null)
Let me know if I can provide more info. I'm very new to multi-threading and trying to get a better handle on this approach.
Thanks,
Josh
In my data loading class I setup the parent/child mocs here to perform
the work on the background thread:
dispatch_queue_t queue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSManagedObjectContext *mainMOC = self.managedObjectContext;
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
You should not do that. Do this instead.
NSManagedObjectContext *mainMOC = self.managedObjectContext;
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
Make sure you access the MOC in a performBlock. For example,
[moc performBlock:^{
// Anything at all involving this MOC or any of its objects
}];
When the json data has completed I attempt to peform a save operation
with the following macro:
Consider saving with something like this. Your completion block will be called when the save has finished.
- (void)saveMOC:(NSManagedObjectContext*)moc
completion:(void(^)(NSError *error))completion {
[moc performBlock:^{
NSError *error = nil;
if ([moc save:&error]) {
if (moc.parentContext) {
return [self saveMOC:moc.parentContext completion:completion];
}
}
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(error);
});
}
}];
}
[self saveMOC:moc completion:^(NSError *error) {
// Completion handler is called from main-thread, after save has finished
if (error) {
// Handle error
} else {
}
}];
EDIT
This code will crash if moc.parentContext is main concurrency type. –
Mundi
There is no inherent reason that the code I posted should cause a crash with a parent MOC of NSMainQueueConcurrencyType. It has supported being a parent context ever since parent/child was added to Core Data.
Maybe I was missing a typo, so I copy/paste saveMOC:completion: straight from this answer, and wrote the following test helper.
- (void)testWithChildConcurrencyType:(NSManagedObjectContextConcurrencyType)childConcurrencyType
parentConcurrencyType:(NSManagedObjectContextConcurrencyType)parentConcurrencyType {
NSAttributeDescription *attr = [[NSAttributeDescription alloc] init];
attr.name = #"attribute";
attr.attributeType = NSStringAttributeType;
NSEntityDescription *entity = [[NSEntityDescription alloc] init];
entity.name = #"Entity";
entity.properties = #[attr];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] init];
model.entities = #[entity];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL];
NSManagedObjectContext *parent = [[NSManagedObjectContext alloc] initWithConcurrencyType:parentConcurrencyType];
parent.persistentStoreCoordinator = psc;
NSManagedObjectContext *child = [[NSManagedObjectContext alloc] initWithConcurrencyType:childConcurrencyType];
child.parentContext = parent;
NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:#"Entity" inManagedObjectContext:child];
[obj setValue:#"value" forKey:#"attribute"];
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:#"save from %# to %# finished", concurrencyTypeString(childConcurrencyType), concurrencyTypeString(parentConcurrencyType)]];
[self saveMOC:child completion:^(NSError *error) {
// Verify data saved all the way to the PSC
NSManagedObjectContext *localMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
localMoc.persistentStoreCoordinator = psc;
NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:#"Entity"];
XCTAssertEqualObjects(#"value", [[[localMoc executeFetchRequest:fr error:NULL] firstObject] valueForKey:#"attribute"]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
And then, I wrote a test for each possible parent/child relationship.
- (void)testThatDoingRecursiveSaveFromPrivateToPrivateWorks {
[self testWithChildConcurrencyType:NSPrivateQueueConcurrencyType
parentConcurrencyType:NSPrivateQueueConcurrencyType];
}
- (void)testThatDoingRecursiveSaveFromPrivateToMainWorks {
[self testWithChildConcurrencyType:NSPrivateQueueConcurrencyType
parentConcurrencyType:NSMainQueueConcurrencyType];
}
- (void)testThatDoingRecursiveSaveFromMainToPrivateWorks {
[self testWithChildConcurrencyType:NSMainQueueConcurrencyType
parentConcurrencyType:NSPrivateQueueConcurrencyType];
}
- (void)testThatDoingRecursiveSaveFromMainToMainWorks {
[self testWithChildConcurrencyType:NSMainQueueConcurrencyType
parentConcurrencyType:NSMainQueueConcurrencyType];
}
So, what am I missing?
As I write this, I am reminded of a 360iDev presentation where the presenter said that you can't call performBlock on a NSMainQueueConcurrencyType context. At the time, I thought he just misspoke, meaning confinement, but maybe there is some confusion in the community about this.
You can't call performBlock on a NSConfinementConcurrencyType MOC, but performBlock is fully supported for NSMainQueueConcurrencyType.
I'm using the following code to download some pic from the server,
-(void)viewDidLoad
{
for (int i = 0 ; i < picsNames.count ; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(i * 320.0, 0.0, 320.0, self.view.frame.size.height)];
imageView.backgroundColor = [UIColor blackColor];
imageView.tag = IMAGE_VIEWS_TAG + i;
[scrollView addSubview:imageView];
//getting the image
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(getImage:) object:imageView];
[queue addOperation:operation];
}
}
-(void)getImage:(UIImageView *)imageView
{
//getting the "image" from the server .....
[self performSelectorOnMainThread:#selector(loadPic:) withObject:[NSArray arrayWithObjects:imageView, image,nil] waitUntilDone:YES];
}
-(void)loadPic:(NSArray *)imageAndImageViewArray
{
//loading the image to imageview
UIImageView *imageView = (UIImageView *)[imageAndImageViewArray objectAtIndex:0];
imageView.image = (UIImage *)[imageAndImageViewArray objectAtIndex:1];
}
After loading some pictures the following line gives memory warning and crashes the app.
[self performSelectorOnMainThread:#selector(loadPic:) withObject:[NSArray arrayWithObjects:imageView, image,nil] waitUntilDone:YES];
I don't know how to solve this issue.
Thanks everyone :)
So after spending too much time on this I realized that the error is due to having too many UIImage objects alloc at the same time no the NSQueue of main NSTread.
So Just changed to code so I have only 10 UIImage objects at the time.
When running the analyze in xcode 4.2 i have the warning "Value stored to 'image' during its initialization is never read." Anyone can help me out what is wrong with the code?
UIImage *image=[[[UIImage alloc] init]autorelease];
if (carousel==recipeCarousel) {
image = [recipeItems objectAtIndex:index];
} else {
image = [packItems objectAtIndex:index];
}
UIView *view = [[[UIImageView alloc] initWithImage:image] autorelease];
return view;
Thank you.
You have alloc the UIImage with this code
UIImage *image=[[[UIImage alloc] init]autorelease];
but later you assign object in recipeItems or packItems
so that you will lose your control to the allocated object.
So you can either do this
UIImage *image = nil;
if (carousel==recipeCarousel) {
image = [recipeItems objectAtIndex:index];
} else {
image = [packItems objectAtIndex:index];
}
or
NSArray *targetItems = nil;
if (carousel==recipeCarousel) {
targetItems = recipeItems;
} else {
targetItems = packItems;
}
UIImage *image = [targetItems objectAtIndex:index];
I'm a bit confused with this as I've seen way too many different variants and not sure which one is the correct way. Currently I have:
- (IBAction)pickImageFromLibrary:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
// [picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 10.0f, 320.0f, 264.0f)];
self.studyView = imageView;
[imageView release];
[self.tableView setTableHeaderView:studyView];
self.fitImage = [ImageHelper image:image fitInView:studyView];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
studyView.image = self.fitImage;
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
I'm allocating the UIImagePickerController in the first method but wouldn't it be logical to only release it in the 2nd method when I dismiss it?
No, because it's retained when presented modally, via presentModelViewController. This is the common pattern you'll find when presenting new view controllers, whether modally, custom view controllers or not. This is fine:
- (IBAction)pickImageFromLibrary:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Good Morning at all,
i have a big problem in the following code and no solution, so i hope someone could
help me:
- (IBAction)goToChart {
[rootViewController switchViews];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *weiter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
weiter.frame = CGRectMake(100, 400, 120, 40);
[weiter addTarget:self action:#selector(goToChart) forControlEvents:UIControlEventTouchUpInside];
NSString *ansicht = #"Weiter";
[weiter setTitle:ansicht forState:UIControlStateNormal];
[self.view addSubview:weiter];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
NSLog(#"%d is the index",i);
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
So you can see there 10 Images in this CoverFlowView, but how could i find out the ACTUAL picture that is in front, to use this in another view??
Could someone help me, please?
Greetings Marco
-(void)openFlowView: (AFOpenFlowView *)openFlowView imageSelected:(int)index
{
AppDelegate_iPhone *appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication]delegate];
db_detail = (DB_data *)[self.GalleryArray objectAtIndex:index];
appDelegate.Id = db_detail.Id;
// NSLog(#"id: value is %d",db_detail.Id);
// NSLog(#"Name vlaue is: %#",db_detail.Name);
appDelegate.title = db_detail.Name;
DetailMovieViewController *ViewController = [[DetailMovieViewController alloc]init];
[self.navigationController pushViewController:ViewController animated:YES ];
[ViewController release];
[db_detail release];
}