Bild zu Tiff mit verschiednen Größen konvertieren.

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Bild zu Tiff mit verschiednen Größen konvertieren.

    Hallo Leute,

    Ich habe ein Array mit URLs von Bildern und möchte diese Bilder als Tiff oder Icon mit verschiedenen Größen (gleiche Auflösung, dpi) speichern. Dazu habe ich mir folgendes überlegt:

    Quellcode

    1. for (NSURL *url in urls) {
    2. NSImage *newIcon = [[NSImage alloc] init];
    3. NSString *fileName = [[url.path lastPathComponent] stringByDeletingPathExtension];
    4. // NSLog(@"Representations: %lu", newIcon.representations.count);
    5. NSImageRep *icon512 = [NSImageRep imageRepWithContentsOfURL:url];
    6. icon512.size = NSMakeSize(512, 512);
    7. icon512.pixelsHigh = 512;
    8. icon512.pixelsWide = 512;
    9. [newIcon addRepresentation:icon512];
    10. NSImageRep *icon256 = [NSImageRep imageRepWithContentsOfURL:url];
    11. icon256.size = NSMakeSize(256, 256);
    12. icon256.pixelsHigh = 256;
    13. icon256.pixelsWide = 256;
    14. [newIcon addRepresentation:icon256];
    15. NSImageRep *icon128 = [NSImageRep imageRepWithContentsOfURL:url];
    16. icon128.size = NSMakeSize(128, 128);
    17. icon128.pixelsHigh = 128;
    18. icon128.pixelsWide = 128;
    19. [newIcon addRepresentation:icon128];
    20. NSImageRep *icon32 = [NSImageRep imageRepWithContentsOfURL:url];
    21. icon32.size = NSMakeSize(32, 32);
    22. icon32.pixelsHigh = 32;
    23. icon32.pixelsWide = 32;
    24. [newIcon addRepresentation:icon32];
    25. NSImageRep *icon16 = [NSImageRep imageRepWithContentsOfURL:url];
    26. icon16.size = NSMakeSize(16, 16);
    27. icon16.pixelsHigh = 16;
    28. icon16.pixelsWide = 16;
    29. [newIcon addRepresentation:icon16];
    30. NSError *saveERROR;
    31. [[newIcon TIFFRepresentation] writeToFile: [[self applicationCustomIconsDirectory].path stringByAppendingFormat:@"%@.tiff", fileName] options:NSDataWritingAtomic error:&saveERROR];
    32. }
    Alles anzeigen


    Allerdings resultiert das in Tiff Dateien, welche fünf mal die gleiche Größe enthalten.

    Was mache ich falsch?


    Gruß und Danke
    Daniel
    “I want to see an elephant hunt down a man for the sole purpose of collecting his teeth, while a chorus of typewriters sings songs that praises the bananas for their wisdom, leadership, and their high levels of potassium.” ― Jarod Kintz, I Want
  • Die Doku zeigt dir warum das oben nicht ging.

    setPixelsHigh:
    Subclasses should call this method when loading image data to notify the parent class of the image height. You cannot use this method to change the actual number of pixels in the image.


    setSize:
    This method determines the size of the image when it’s rendered. It is not necessarily the same as the width and height of the image in pixels as specified by the image data


    Unter iOS würde ich einen neuen Context erstellen und das Bild in neuer Größe da rein zeichnen.
    Unter OS X müssen dir andere helfen. ;)