2013-10-30 23 views
0

我使用此代码将大小更改为图像,然后使用新维度创建一个新文件。所有工作正常,但它改变了图像的dpi分辨率,我不希望这...初始图像dpi res是328,并在调整大小后,它成为72 ...如何保持原来的dpi分辨率? 这里是我的代码:在可可:为什么改变图像大小,它也改变图像分辨率?

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

任何帮助将非常感谢!谢谢...马西

+0

源图像的维度是什么? – trojanfoe

+0

1024 x 1024它发生在PNG文件以及PSD文件 – Blue

+1

这有帮助吗? http://stackoverflow.com/questions/8048597/how-to-change-image-resolution-in-objective-c – trojanfoe

回答

0

最后,我已经能够更改使用由trojanfoe指出后所采取的代码...的资源......我已将此添加到我的代码之前写的文件:

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

现在唯一的问题是,最终的决议是321,894而不是328 ......但它已经是一些东西!