简体   繁体   English

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

[英]Resizing image and saving it to the specified directory path in Cocoa

Using this code I am trying to resize the selected image and then want to save it to a specific path: 使用此代码,我尝试调整所选图像的大小,然后将其保存到特定路径:

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

}

My issue is I am not getting that how to save it to thumbnailDirPath. 我的问题是我不知道如何将其保存到thumbnailDirPath。

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);

Check the documentation for NSJPEGFileType as it will show you the other foramt options for saving, such as PNG. 检查NSJPEGFileType的文档,因为它会显示其他用于保存的格式选项,例如PNG。

you should do export your image into file. 您应该将图像导出到文件中。 currently I only see how to store the TIFF image. 目前,我只看到如何存储TIFF图像。

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

Where outputPathName is the path with file name for your thumbnail file. 其中outputPathName是带有缩略图文件名的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM