调整图像大小并将其保存到Cocoa中的指定目录路径

时间:2012-03-14 11:31:28

标签: objective-c xcode cocoa nsimage

使用此代码我尝试调整所选图像的大小,然后将其保存到特定路径:

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

    NSImage * img = [NSImage imageNamed:inputPath];
    [img setSize: NSMakeSize(imageWidth,imageHeight)];

}

-(void)startProcessingImages {

    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:@".jpg"] retain];

    [self processImage:inputFilePath: 66  :55  :thumbnailDirPath];
    [self processImage:inputFilePath: 800 :600 :thumbnailDirPath];
    [self processImage:inputFilePath: 320 :240 :thumbnailDirPath];

}

我的问题是我没有得到如何将其保存到thumbnailDirPath。

2 个答案:

答案 0 :(得分:0)

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.8] forKey:NSImageCompressionFactor];    
NSData *tiffData = [img TIFFRepresentation];
NSData *JPEGData = [[NSBitmapImageRep imageRepWithData:tiffData] representationUsingType:NSJPEGFileType properties:options];
NSError *anError;
if (![JPEGData outputPath options:0 error:&anError])
                MyLog(@"Error saving image: %@ to: %@", anError, outputPath);

查看NSJPEGFileType的文档,因为它会显示其他格式的保存选项,例如PNG。

答案 1 :(得分:-1)

您应该将图片导出到文件中。 目前我只看到如何存储TIFF图像。

[[img TIFFRepresentation] writeToFile:outputPathName atomacally:NO];

其中outputPathName是包含缩略图文件文件名的路径。