0

I have two methods. From the first method I am sending an array to the second method. In my first method [array count] value is 2. But in the seconf method the value is 1. But it should be the same in both method. I know its a silly mistake. But I dont understand exactly where I have done the mistake.

First method:

-(void)uploadOverlayManually: (NSMutableArray *)path{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Array count #1: %d",[path count]);

for (int i =0; i < [path count]; i++) {

    imagePath = [path objectAtIndex:i];

    NSString *infoPath = [[imagePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"info"];
    NSData *infoData = [[NSMutableData alloc] initWithContentsOfFile:infoPath];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:infoData];
    MediaInformation *currentInfo = [unarchiver decodeObjectForKey:@"info"];
    [unarchiver finishDecoding];

    UIImage *baseImage = [UIImage imageWithContentsOfFile:imagePath];
    NSData *data = [root addImageOverlay:baseImage withInfo:currentInfo andPath:imagePath];

    [data writeToFile:imagePath atomically:YES];

    fileUpload = [[NSMutableArray alloc] init];

    [fileUpload addObject:imagePath];

}

[self upload:fileUpload];

}

Second method:

-(void)upload:(NSArray*)filePaths{

if (![[DBSession sharedSession] isLinked]) {

    [[DBSession sharedSession] linkFromController:root]; //root
}

NSLog(@"Array count #2: %d",[filePaths count]);

}

3 Answers 3

1

This line

fileUpload = [[NSMutableArray alloc] init];

inside your loop recreates the array.

It should be before the loop so you add an object on each iteration.

Sign up to request clarification or add additional context in comments.

Comments

0

You instantiate a new array on each loop so it can always be just 1

To achieve the intended behavior use (moved instantiation out of the loop):

-(void)uploadOverlayManually: (NSMutableArray *)path{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Array count #1: %d",[path count]);

fileUpload = [[NSMutableArray alloc] init];

for (int i =0; i < [path count]; i++) {

    imagePath = [path objectAtIndex:i];

    NSString *infoPath = [[imagePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"info"];
    NSData *infoData = [[NSMutableData alloc] initWithContentsOfFile:infoPath];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:infoData];
    MediaInformation *currentInfo = [unarchiver decodeObjectForKey:@"info"];
    [unarchiver finishDecoding];

    UIImage *baseImage = [UIImage imageWithContentsOfFile:imagePath];
    NSData *data = [root addImageOverlay:baseImage withInfo:currentInfo andPath:imagePath];

    [data writeToFile:imagePath atomically:YES];

    [fileUpload addObject:imagePath];

}

[self upload:fileUpload];

Comments

0

You are allocating Array inside of a loop please allocate it in viewDidLoad or in any function that only called once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.