繁体   English   中英

在Mac OS X App Cocoa中生成图像文件的问题

[英]issue in generating image files in mac os x app cocoa

-(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个文件的大小均相同,但没有传递给函数的尺寸或宽度/长度。

可能是什么问题?

NSImage对象是不可变的。 因此,更改image大小时不会对其进行修改。

您应该使用类似下面的代码(从此处改编)。

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

暂无
暂无

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

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