2012-03-16 61 views
0
-(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]; 

} 

我現在面臨的問題是,在上面的代碼生成3個文件具有不同的名稱(如我已定義的名稱應),其具有所有相同的尺寸3個文件,但不包含我傳遞給函數的尺寸或寬度/長度。問題在Mac OS X的應用程序可可生成圖像文件

可能是什麼問題?

回答

1

NSImage對象是不可變的。因此,當您更改其大小時,image未被修改。

你應該使用類似下面的代碼(改編自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]; 
} 
+0

謝謝@sch [resizedImage lockFocus];這是什麼resizedImage?有未申報的標識符錯誤 – 2012-03-16 11:41:05

+1

我重命名了變量,但忘記修改該行:)但現在已經得到糾正。 – sch 2012-03-16 11:46:36

+0

Thanx其真棒,另外一個問題,我們如何保存與不同的名稱源圖像圖像。就像在上面的代碼中,你已經建議我們從源路徑獲取源圖像。我想用不同的名稱保存相同的圖像。可能嗎 ? – 2012-03-16 12:37:12

相關問題