3

Okay... I want to save the visible rectangle of the IKImageView image only. My problem is that, somehow, if I had an image in portrait mode it will be not saved in the right orientation. I drawing the image with this code:

[sourceImg drawInRect:targetRect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];

On the other side if I draw the image with this code:

[sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];

It will be in the right orientation, but edits like zooming are lost. I mean it will save the right cut out, but unfortunately not in the right size.

Here is the code how I load the image:

    - (IBAction)imageSelectionButtonAction:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    id model = [self getModel];

    if (imageView) {
        [panel setAllowedFileTypes:[NSImage imageFileTypes]];
        [panel beginSheetModalForWindow:[[NSApplication sharedApplication] mainWindow] completionHandler:^(NSInteger returnCode) {
            if (returnCode == 1) {
                NSURL *imageUrl = [panel URL];
                CGImageRef          image = NULL;
                CGImageSourceRef    isr = CGImageSourceCreateWithURL( (__bridge CFURLRef)imageUrl, NULL);

                if (isr) {
                    NSDictionary *options = [NSDictionary dictionaryWithObject: (id)kCFBooleanTrue  forKey: (id) kCGImageSourceShouldCache];
                    image = CGImageSourceCreateImageAtIndex(isr, 0, (__bridge CFDictionaryRef)options);

                    if (image) {
                        _imageProperties = (__bridge_transfer NSDictionary*)CGImageSourceCopyPropertiesAtIndex(isr, 0, (__bridge CFDictionaryRef)_imageProperties);
                        _imageUTType = (__bridge NSString*)CGImageSourceGetType(isr);
                    }
                    CFRelease(isr);
                }

                if (image) {
                    [imageView setImage:image imageProperties:_imageProperties];
                    CGImageRelease(image);
                }
                [[model saveTempMutabDict] setValue:[imageUrl absoluteString] forKey:@"tempImage"];
            }   
        }];
        return;
    }
}

And here is the code how I save it:

    - (void)saveImage:(NSString *)path  {
    // get the current image from the image view

    CGImageRef sourceImgRef = [imageView image];
    NSRect targetRect = [imageView visibleRect];
    NSImage *sourceImg = [[NSImage alloc] initWithCGImage:sourceImgRef size:NSZeroSize];

    NSMutableDictionary *thisTiffDict = [_imageProperties valueForKey:@"{TIFF}"];
    NSInteger theOrientation = [[thisTiffDict valueForKey:@"Orientation"] integerValue];
    NSImage *targetImg = nil;
    if (theOrientation == 6) {
        targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.height, [imageView frame].size.width)];
    } else {
        targetImg = [[NSImage alloc] initWithSize:NSMakeSize([imageView frame].size.width, [imageView frame].size.height)];
    }

    NSRect sourceRect = [imageView convertViewRectToImageRect:targetRect];

    [targetImg lockFocus];
    [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
    [sourceImg drawAtPoint:NSZeroPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0f];
    [targetImg unlockFocus];

    _saveOptions = [[IKSaveOptions alloc] initWithImageProperties:_imageProperties imageUTType: _imageUTType];
    NSString * newUTType = [_saveOptions imageUTType];

    CGImageRef targetImgRef = [targetImg CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil];

    if (targetImgRef) {
        NSURL *url = [NSURL fileURLWithPath: path];
        CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)newUTType, 1, NULL);

        if (dest) {
            CGImageDestinationAddImage(dest, targetImgRef, (__bridge CFDictionaryRef)[_saveOptions imageProperties]);
            CGImageDestinationFinalize(dest);
            CFRelease(dest);
        }
    }
}

I have no idea what I'm doing wrong?

Thank you Jens

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
xnz
  • 63
  • 1
  • 5

0 Answers0