是否可以调整粘贴板中图像的大小 xcode 7.2 Objective C?
Is it possible to re-size a image in a uipasteboard xcode 7.2 Objective C?
我目前在 Xcode 7.2 中使用下面的代码,它获取图像并将其粘贴到 imessages 等中。但是它太大(尺寸)。这张图片可以缩小吗?
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *imgData = UIImagePNGRepresentation(image);
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
当它在粘贴板中时不会。但是,如果您担心在将它粘贴到某处时它的大小不正确,请不要害怕,Apple 对我们很好,并为我们制作了很多演示文稿 "resizing"。
如果您确实需要调整图像大小,您需要做的是:
1) 偷懒并使用可以恢复的代码...也就是说,我相信我在这里找到了这个:
- (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;
}
然后在您提供给我们的代码中:
*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]];
如果粘贴到其他地方,它之前已经调整过大小。
我目前在 Xcode 7.2 中使用下面的代码,它获取图像并将其粘贴到 imessages 等中。但是它太大(尺寸)。这张图片可以缩小吗?
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *imgData = UIImagePNGRepresentation(image);
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
当它在粘贴板中时不会。但是,如果您担心在将它粘贴到某处时它的大小不正确,请不要害怕,Apple 对我们很好,并为我们制作了很多演示文稿 "resizing"。
如果您确实需要调整图像大小,您需要做的是:
1) 偷懒并使用可以恢复的代码...也就是说,我相信我在这里找到了这个:
- (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;
}
然后在您提供给我们的代码中:
*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]];
如果粘贴到其他地方,它之前已经调整过大小。