如何从IKImageView保存图像?

时间:2012-02-16 11:24:22

标签: objective-c nsimage ikimageview

好的......我只想保存IKImageView图像的可见矩形。 我的问题是,不知何故,如果我在纵向模式下有一个图像,它将不会以正确的方向保存。我用这段代码绘制图像:

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

另一方面,如果我使用此代码绘制图像:

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

它将处于正确的方向,但缩放等编辑将丢失。我的意思是它会保留正确的剪切,但遗憾的是尺寸不合适。

以下是我加载图片的代码:

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

以下是我保存它的代码:

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

我不知道我做错了什么?

谢谢 延

0 个答案:

没有答案