2012-03-16 4 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 app 코코아에서 이미지 파일 생성시 문제가 발생했습니다.

문제점은 무엇입니까?

답변

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 무엇입니까? 선언되지 않은 식별자 오류가 있습니다. –

+1

변수의 이름을 변경했지만 해당 행을 수정하지 않았습니다. :) 그러나 이제는 수정되었습니다. – sch

+0

두려운 것, 또 하나의 질문입니다. 소스 이미지 이미지를 어떻게 다른 이름으로 저장할 수 있습니까? 위의 코드에서와 마찬가지로 소스 경로에서 소스 이미지를 가져 오는 중입니다. 같은 이미지를 다른 이름으로 저장하고 싶습니다. 가능한가? –