Objective c 在mac os x应用程序中生成图像文件时出现问题 -(void)processImage:(NSString*)inputPath:(int)imageWidth:(int)imageHeight:(NSString*)outputPath{ //NSImage*img=[NSImage ImageName:inputPath]; NSImage*image=[[NSImage alloc]initWithContentsOfFile:inputPath]; [图像设置大小:NSMakeSize(图像宽度、图像高度)]; [[image TIFFRepresentation]writeToFile:outputPath原子化:否]; NSLog(@“已创建图像文件”); } -(iAction)进程映像:(id)发送方{ NSTimeInterval时间戳=[[NSDate date]TimeIntervalnce1970]; //NSTimeInterval定义为双精度 NSNumber*timeStampObj=[NSNumber numberwhithint:timeStamp]; NSNumberFormatter*格式化程序=[[NSNumberFormatter alloc]init]; [格式化程序setNumberStyle:NSNumberFormatterNoStyle]; NSString*convertNumber=[formatter stringForObjectValue:timeStampObj]; NSLog(@“timeStampObj::%@),convertNumber); fileNameNumber=[[convertNumber stringByAppendingString:[self-genRandStringLength:8]]retain]; int i;//循环计数器。 //循环浏览所有文件并处理它们。 对于(i=0;i

Objective c 在mac os x应用程序中生成图像文件时出现问题 -(void)processImage:(NSString*)inputPath:(int)imageWidth:(int)imageHeight:(NSString*)outputPath{ //NSImage*img=[NSImage ImageName:inputPath]; NSImage*image=[[NSImage alloc]initWithContentsOfFile:inputPath]; [图像设置大小:NSMakeSize(图像宽度、图像高度)]; [[image TIFFRepresentation]writeToFile:outputPath原子化:否]; NSLog(@“已创建图像文件”); } -(iAction)进程映像:(id)发送方{ NSTimeInterval时间戳=[[NSDate date]TimeIntervalnce1970]; //NSTimeInterval定义为双精度 NSNumber*timeStampObj=[NSNumber numberwhithint:timeStamp]; NSNumberFormatter*格式化程序=[[NSNumberFormatter alloc]init]; [格式化程序setNumberStyle:NSNumberFormatterNoStyle]; NSString*convertNumber=[formatter stringForObjectValue:timeStampObj]; NSLog(@“timeStampObj::%@),convertNumber); fileNameNumber=[[convertNumber stringByAppendingString:[self-genRandStringLength:8]]retain]; int i;//循环计数器。 //循环浏览所有文件并处理它们。 对于(i=0;i,objective-c,xcode,cocoa,nsimage,Objective C,Xcode,Cocoa,Nsimage,我面临的问题是,上面的代码生成的3个文件具有不同的名称(正如我定义的名称应该是的),与所有3个文件的大小相同,但与传递给函数的尺寸或宽度/长度不同 有什么问题吗?NSImage对象是不可变的。因此,当您更改图像的大小时,图像不会被修改 您应该使用如下代码(改编自) 谢谢@sch[resizedImage lockFocus];这是什么尺寸的图像?有一个未声明的标识符错误我重命名了变量,但忘了修改那一行:),但现在已经更正了。Thanx太棒了,还有一个问题,我们如何用不同的名称保存源图像。就像在上

我面临的问题是,上面的代码生成的3个文件具有不同的名称(正如我定义的名称应该是的),与所有3个文件的大小相同,但与传递给函数的尺寸或宽度/长度不同


有什么问题吗?

NSImage
对象是不可变的。因此,当您更改图像的大小时,
图像
不会被修改

您应该使用如下代码(改编自)


谢谢@sch[resizedImage lockFocus];这是什么尺寸的图像?有一个未声明的标识符错误我重命名了变量,但忘了修改那一行:),但现在已经更正了。Thanx太棒了,还有一个问题,我们如何用不同的名称保存源图像。就像在上面的代码中,您建议我们从源路径获取源图像。我想用不同的名称保存相同的图像。可能吗?
[[sourceImage TIFFRepresentation]writeToFile:newPath原子化:否]thanx已经这么做了,但是通过你建议的代码,你的天才:)
-(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];

}
-(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];
}