Search code examples
objective-cxcodecocoansimage

issue in generating image files in mac os x app cocoa


-(void)processImage:(NSString*)inputPath:(int)imageWidth:(int)imageHeight:(NSString*)outputPath {

 //   NSImage * img = [NSImage imageNamed:inputPath];

    NSImage *image = [[NSImage alloc] initWithContentsOfFile:inputPath];

    [image setSize: NSMakeSize(imageWidth,imageHeight)];

    [[image TIFFRepresentation] writeToFile:outputPath atomically:NO];

    NSLog(@"image file created");

}
- (IBAction)processImage:(id)sender {

    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    // NSTimeInterval is defined as double
    NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterNoStyle];

    NSString *convertNumber = [formatter stringForObjectValue:timeStampObj];

    NSLog(@"timeStampObj:: %@", convertNumber);

    fileNameNumber = [[convertNumber stringByAppendingString:[self genRandStringLength:8]] retain];

    int i; // Loop counter.

    // Loop through all the files and process them.
    for( i = 0; i < [files count]; i++ )
    {
        inputFilePath = [[files objectAtIndex:i] retain];
        NSLog(@"filename::: %@", inputFilePath);

        // Do something with the filename.

        [selectedFile setStringValue:inputFilePath];

        NSLog(@"selectedFile:::: %@", selectedFile);
    }

    NSLog(@"curdir:::::%@", inputFilePath);

    NSString *aString = [[NSString stringWithFormat:@"%@%@%@", thumbnailDirPath , @"/" , fileNameNumber] retain];

    fileNameJPG = [[aString stringByAppendingString:@"_small.jpg"] retain];
    fileNameJPG1 = [[aString stringByAppendingString:@".jpg"] retain];
    fileNameJPG2 = [[aString stringByAppendingString:@"_H.jpg"] retain];

        [self processImage:inputFilePath: 66  :55  :fileNameJPG];

        [self processImage:inputFilePath: 800 :600 :fileNameJPG1];

        [self processImage:inputFilePath: 320 :240 :fileNameJPG2];

}

The issue I am facing is that the above code is generating 3 files with different names(as I have defined the name should be) having the same size of all 3 files but not with the dimensions or width/length I pass to the function.

What can be the issue ?


Solution

  • NSImage objects are immutable. So image is not modified when you change its size.

    You should use something like the following code (adapted from here).

    -(void)saveImageAtPath:(NSString*)sourcePath toPath:(NSString*)targetPath withWidth:(int)targetWidth andHeight:(int)targetHeight
    {
        NSImage *sourceImage = [[NSImage alloc] initWithContentsOfFile:sourcePath];
        NSImage *targetImage = [[NSImage alloc] initWithSize: NSMakeSize(targetWidth, targetHeight)];
    
        NSSize sourceSize = [sourceImage size];
        NSRect sourceRect = NSMakeRect(0, 0, sourceSize.width, sourceSize.height);
        NSRect targetRect = NSMakeRect(0, 0, targetWidth, targetWidth);
    
        [targetImage lockFocus];
        [sourceImage drawInRect:targetRect fromRect:sourceRect  operation: NSCompositeSourceOver fraction: 1.0];
        [targetImage unlockFocus];
    
        [[targetImage TIFFRepresentation] writeToFile:targetPath atomically:NO];
        NSLog(@"image file created");
        [sourceImage release];
        [targetImage release];
    }