Frage

I am trying to open an image with open dialog, resize it and save it with a new name, I found some code in other posts and putting 2 or 3 things togheter I finished having this code, but it doesn't work... here is my code:

  -(IBAction)apriFileImmagine:(id)sender
{   
[pannelloHome makeKeyAndOrderFront:self];

int i; // Loop counter.

// Create the File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths objectAtIndex:0];
NSURL *myUrl = [NSURL fileURLWithPath:documentsDirectory2];
[openDlg setDirectoryURL:myUrl];

// Enable the selection of files in the dialog.
[openDlg setCanChooseFiles:YES];

// Enable the selection of directories in the dialog.
[openDlg setCanChooseDirectories:YES];



// Display the dialog.  If the OK button was pressed,
// process the files.
if ( [openDlg runModal] == NSOKButton )
{
    // Get an array containing the full filenames of all
    // files and directories selected.
    NSArray* files = [openDlg URLs];

    // Loop through all the files and process them.
    for( i = 0; i < [files count]; i++ )
    {
        NSURL* fileName = [files objectAtIndex:i];

         [self scaleIcons:documentsDirectory2 :fileName];

    }
 }
}


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

 NSImage *image = [NSImage imageNamed:[NSString stringWithFormat:@"%@",nomeImmagine]];

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

as you can see I'm trying to save in the same directory where the image has been taken... but all I'm getting is an error:

: ImageIO: CGImageSourceCreateWithData data parameter is nil

Anyone knows what I am doing wrong? Any help will be very much appreciated... thanks Massy

War es hilfreich?

Lösung

The problem with your image only, image is nil, So I have modified this method:

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

     //NSImage *image = [NSImage imageNamed:[NSString stringWithFormat:@"%@",nomeImmagine]]; commented this part
//start modification
    NSImage     *image = [[NSImage alloc] initWithContentsOfFile:[[nomeImmagine path] autorelease]];
    if (!image)
        image = [[NSWorkspace sharedWorkspace] iconForFile:[nomeImmagine path]];
//end modification

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

Try This , it worked for me..

Andere Tipps

Yes that's wrong. Try:

NSImage *image = [[NSImage alloc] initWithContentsOfFile:nomeImage];
NSAssert(image, @"Image is NOT valid");

This should then return a valid NSImage.... ? Depending on if you're using ARC you may want to release/auto release as you have 'alloc'd.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top