是否可以在uipasteboard xcode 7.2 Objective C中调整图像大小? [英] Is it possible to re-size a image in a uipasteboard xcode 7.2 Objective C?

查看:113
本文介绍了是否可以在uipasteboard xcode 7.2 Objective C中调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Xcode 7.2中使用以下代码,该代码会拍摄图像并将其粘贴到图片等中.但是它太大(尺寸).可以缩小此图像吗?

I’m currently using this code bellow in Xcode 7.2, which takes the image and pastes it in imessages etc. But it’s too large (in dimensions). Is it possible to make this image smaller?

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSData *imgData = UIImagePNGRepresentation(image);
    [pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]]; 

推荐答案

不在粘贴板中.但是,如果您担心将它粘贴到某个地方时尺寸不正确,请不要担心,Apple对我们很友善,并且已经为我们构建了很多调整大小"的演示文稿.

Not whilst it's in the pasteboard. BUT, if you're worried about it being the wrong size when you paste it somewhere, FEAR NOT, Apple have been kind to us and have built quite a lot of the presentation "resizing" into things for us.

如果真的需要调整图像大小,您需要做的是:

What you need to do if you really HAVE to resize the image is:

1)懒惰并使用可以恢复的代码...即,我相信我在这里找到了该代码:

1) Be lazy and use code which you can resume... i.e. I found this on here I believe:

- (NSImage *)imageResize:(NSImage*)anImage newSize:(NSSize)newSize {
    NSImage *sourceImage = anImage;
    [sourceImage setScalesWhenResized:YES];

    // Report an error if the source isn't a valid image
    if (![sourceImage isValid]){
        NSLog(@"Invalid Image");
    } else {
        NSImage *smallImage = [[NSImage alloc] initWithSize: newSize];
        [smallImage lockFocus];
        [sourceImage setSize: newSize];
        [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
        [sourceImage drawAtPoint:NSZeroPoint fromRect:CGRectMake(0, 0, newSize.width, newSize.height) operation:NSCompositeCopy fraction:1.0];
        [smallImage unlockFocus];
        return smallImage;
    }
    return nil;
}

然后输入您提供给我们的一些代码:

then in the bit of code which you've given us:

    *pasteboard = [UIPasteboard generalPasteboard];
    NSImage *myShinyResizedImage = [self imageResize: oldImage newSize: CGSizeMake(100.0, 100.0)];
    NSData *imgData = UIImagePNGRepresentation(myShinyResizedImage);
    [pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];

如果不打算粘贴到其他位置,则需要重新调整大小.

And it's been resized before if goes off to get pasted elsewhere.

这篇关于是否可以在uipasteboard xcode 7.2 Objective C中调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆