Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

Is it possible to re-size a image in a uipasteboard xcode 7.2 Objective C?

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]]; 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...