Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c cocoa:为什么改变图像大小也会改变图像分辨率?_Objective C_Cocoa_Image Resizing - Fatal编程技术网

Objective c cocoa:为什么改变图像大小也会改变图像分辨率?

Objective c cocoa:为什么改变图像大小也会改变图像分辨率?,objective-c,cocoa,image-resizing,Objective C,Cocoa,Image Resizing,我正在使用此代码更改图像的大小,然后创建一个具有新尺寸的新文件。一切都很好,但它改变了图像的dpi分辨率,我不想这样。。。初始图像dpi分辨率为328,调整大小后变为72。。。如何保持原来的新闻部决议? 这是我的密码: - (void)scaleIcons:(NSString *)outputPath :(NSURL *)nomeImmagine { NSImage *image = [[NSImage alloc] initWithContentsOfFile:[nomeImmagine

我正在使用此代码更改图像的大小,然后创建一个具有新尺寸的新文件。一切都很好,但它改变了图像的dpi分辨率,我不想这样。。。初始图像dpi分辨率为328,调整大小后变为72。。。如何保持原来的新闻部决议? 这是我的密码:

- (void)scaleIcons:(NSString *)outputPath :(NSURL *)nomeImmagine
 {

 NSImage *image = [[NSImage alloc] initWithContentsOfFile:[nomeImmagine path]];
if (!image)
    image = [[NSWorkspace sharedWorkspace] iconForFile:[nomeImmagine path]];

NSSize outputSize = NSMakeSize(512.0f,512.0f);
NSImage *anImage  = [self scaleImage:image toSize:outputSize];

NSString *finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];
NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}



- (NSImage *)scaleImage:(NSImage *)image toSize:(NSSize)targetSize
{
if ([image isValid])
{
    NSSize imageSize = [image size];
    float width  = imageSize.width;
    float height = imageSize.height;
    float targetWidth  = targetSize.width;
    float targetHeight = targetSize.height;
    float scaleFactor  = 0.0;
    float scaledWidth  = targetWidth;
    float scaledHeight = targetHeight;

    NSPoint thumbnailPoint = NSZeroPoint;

    if (!NSEqualSizes(imageSize, targetSize))
    {
        float widthFactor  = targetWidth / width;
        float heightFactor = targetHeight / height;

        if (widthFactor < heightFactor)
        {
            scaleFactor = widthFactor;
        }
        else
        {
            scaleFactor = heightFactor;
        }

        scaledWidth  = width  * scaleFactor;
        scaledHeight = height * scaleFactor;

        if (widthFactor < heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }

        else if (widthFactor > heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }

        newImage = [[NSImage alloc] initWithSize:targetSize];

        [newImage lockFocus];

        NSRect thumbnailRect;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width = scaledWidth;
        thumbnailRect.size.height = scaledHeight;

        [image drawInRect:thumbnailRect
                 fromRect:NSZeroRect
                operation:NSCompositeSourceOver
                 fraction:1.0];

        [newImage unlockFocus];
    }


 }

return newImage;
}
-(void)刻度符号:(NSString*)输出路径:(NSURL*)命名引擎
{
NSImage*image=[[NSImage alloc]initWithContentsOfFile:[nomeimengine path]];
如果(!图像)
image=[[NSWorkspace sharedWorkspace]图标文件:[NomeImEngine路径]];
NSSize outputSize=NSMakeSize(512.0f,512.0f);
NSImage*anImage=[自缩放图像:图像大小:输出大小];
NSString*finalPath=[outputPath stringByAppendingString:@”/icon512x512.png];
NSData*图像数据=[anImage TIFFRepresentation];
NSBitmapImageRep*rep=[NSBitmapImageRep imageRepWithData:imageData];
NSData*dataToWrite=[rep representationUsingType:NSPNGFileType属性:nil];
[dataToWrite WriteFile:最终路径原子:否];
}
-(NSImage*)缩放图像:(NSImage*)图像大小:(NSSize)目标大小
{
如果([图像有效])
{
NSSize imageSize=[图像大小];
浮动宽度=imageSize.width;
浮动高度=imageSize.height;
float targetWidth=targetSize.width;
浮动目标高度=targetSize.height;
float scaleFactor=0.0;
浮动缩放宽度=目标宽度;
浮动缩放高度=目标高度;
NSPoint thumbnailPoint=NSZeroPoint;
如果(!nSequalizes(imageSize,targetSize))
{
浮动宽度系数=目标宽度/宽度;
浮动高度系数=目标高度/高度;
if(宽度系数<高度系数)
{
scaleFactor=宽度因子;
}
其他的
{
比例因子=高度因子;
}
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
if(宽度系数<高度系数)
{
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}
否则如果(宽度因子>高度因子)
{
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
newImage=[[NSImage alloc]initWithSize:targetSize];
[新图像锁定焦点];
NSRect thumbnailRect;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[图像drawInRect:thumbnailRect
fromRect:NSZeroRect
操作:NSCompositeSourceOver
分数:1.0];
[newImage unlockFocus];
}
}
返回新图像;
}

任何帮助都将不胜感激!谢谢Massy

最后,我终于可以改变分辨率了。。。使用特洛伊木马指向的帖子获取的代码。。。在编写文件之前,我在代码中添加了以下内容:

NSSize pointsSize = rep.size;
NSSize pixelSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);

CGFloat currentDPI = ceilf((72.0f * pixelSize.width)/pointsSize.width);
NSLog(@"current DPI %f", currentDPI);

NSSize updatedPointsSize = pointsSize;

updatedPointsSize.width = ceilf((72.0f * pixelSize.width)/328);
updatedPointsSize.height = ceilf((72.0f * pixelSize.height)/328);

[rep setSize:updatedPointsSize];

现在唯一的问题是最终决议是321894而不是328。。。但这已经很重要了

源图像的尺寸是多少?1024 x 1024它发生在png文件和psd文件中,这有帮助吗?我已经尝试过使用它,但我不知道如何将它集成到我的代码中。。。你…吗?