welcome to linkAR technical documentation

previous

Add Images into ImageTracker library:

In this project, we will see the edited images, but in this picture, we have to distinguish between the original image and the overlay image, although these images are associated. The original image is the image that the user takes with the camera and the overlay image is an image with a clear color background containing only the objects that the user has created when he has edited the original image. Now, we will see these two types of images with more details.

4.1.- Original image


When the user has selected the cropping area, the image that is contained in it, is saved as original image.
In CropController.m we have:

- (IBAction)doneClicked:(id)sender {
 
    MainNavigationController* navController = (MainNavigationController*)[self parentViewController];
 
    //Add the cropped image
    int imgId = [navController._trackingController addImage:[imageCropper getCroppedImage]];
    if (imgId > -1) {
        [self performSegueWithIdentifier:@"crop" sender:sender];
        navController._sessionImageId = imgId;
    }
    else
    {
        //If the cropped image does not include details, it shows an alert to select a new crop rectangle.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not enough details"
                                                        message:@"Please try cropping again to include more details."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        navController._sessionImageId = -1;
    }
}

In TrackingViewController.m we save the original image and add it into Tracker library:

-(int)  addImage:(UIImage*)img
{
    //Add the original image into library and get its number.
    NSNumber* uId = [_tracker addImage:img];
    if (uId.intValue > -1) {
        //If the image has been added into library, it saves the image into Documents.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ORG"];
        NSError* error;
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
 
        NSString* fileName = [dataPath stringByAppendingPathComponent:uId.stringValue];
        fileName = [fileName stringByAppendingPathExtension:@"png"];
 
        [UIImagePNGRepresentation(img) writeToFile:fileName atomically:YES];
 
    }
    return uId.intValue;
}


4.2.- Overlay image


When the user finishes to edit the image, the overlay image is saved.
In DrawController.m we have:

- (IBAction)doneClicked:(id)sender {
    //  save and add to DB  _drawView.image
    [self endEditText];
    [self endEditSticker];
 
    if ([_undoArray count] == 0) {
        //If the overlay image is empty, it shows an alert.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You'r drawing is empty"
                                                        message:@"You cannot save an empty drawing"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }else{
        if ([[self parentViewController] class] == [MainNavigationController class]) {
            MainNavigationController* navController = (MainNavigationController*)[self parentViewController];
            //Saves the overlay image with original image Id.
            [navController._trackingController setOverlay:[self.drawView image] forId:navController._sessionImageId];
        }
 
 
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


In TrackingViewController.m we save the overlay image in Documents and set its name in Tracked library. We use the same Id that we had used with the original image.

-(void) setOverlay:(UIImage*)img forId:(int)uId
{
    //Id of original image
    NSNumber* orgImgId = [NSNumber numberWithInt:uId];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"OVER"];
    NSError* error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
 
    NSString* fileName = [dataPath stringByAppendingPathComponent:orgImgId.stringValue];
    fileName = [fileName stringByAppendingPathExtension:@"png"];
 
    [UIImagePNGRepresentation(img) writeToFile:fileName atomically:YES];
 
    //Set name of overlay image in library with original image Id.
    [_tracker setTrackedImageNamed:fileName forImageId:orgImgId];
}


4.3.- Loading images from Documents


When the application runs, TrackingViewController.m loads the originals images from Documents. How we can see in the following code, when an original image is loaded, it adds it into Tracker library and sets the name of the overlay image in the library.

-(void)loadImagesFromDocuments
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"ORG"];
    NSError* error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
 
    NSArray* orgFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:&error];
    for (NSString* filePath in orgFiles) {
        //Original image
        UIImage* orgImg = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",dataPath,filePath]];
 
        //Add original image into library
        NSNumber* uId = [_tracker addImage:orgImg];
 
        //Set name of overlay image in Tracked library
        [_tracker setTrackedImageNamed:[NSString stringWithFormat:@"%@/OVER/%@",documentsDirectory,filePath] forImageId:uId];
    }
}



For example, in ImagesTableViewController we can distinguish between these two images:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    //Original image
    [cell._orgImageView setImage:[_imagesArray objectAtIndex:indexPath.row]];
 
    //Overlay image
    [cell._overlayImageView setImage:[_overlayImagesArray objectAtIndex:indexPath.row]];
    ...
}

previous