gpt4 book ai didi

iOS Action 扩展如何获取小图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:24 25 4
gpt4 key购买 nike

在系统图片库中,打开我的 Action 扩展,因为图片太大,扩展崩溃了,请问如何调小一点的图片?

我尝试使用以下代码,但它不起作用。

NSDictionary * dict =@{NSItemProviderPreferredImageSizeKey:[NSValue valueWithCGSize:CGSizeMake(300, 300)]};
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:dict completionHandler:^(UIImage *image, NSError *error) {

}];

最佳答案

在将图像加载到 imageView 之前。首先调整它的大小。然后在 imageView 上加载调整大小的图像。

UIImage *img = [UIImage imageWithData:selectedImgdata];
selectedImage = [self resizeImage:img];

调整图像数据:

-(UIImage *)resizeImage :(UIImage *)theImage
{
if((theImage.size.width > 1024 && theImage.size.height > 768) || (theImage.size.width > 768 && theImage.size.height > 1024) || (theImage.size.width > 1024 && theImage.size.height < 768) || (theImage.size.height > 1024 && theImage.size.width < 768))
{
float newHeight,newWidth,oldWidth,oldHeight,scaleFactor;

if(theImage.size.width > theImage.size.height)
{
oldWidth = theImage.size.width;
scaleFactor = 1024 / oldWidth;

newHeight = theImage.size.height * scaleFactor;
newWidth = oldWidth * scaleFactor;
}
else
{
oldHeight = theImage.size.height;
scaleFactor = 1024 / oldHeight;

newWidth = theImage.size.width * scaleFactor;
newHeight = oldHeight * scaleFactor;
}

UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth, newHeight), NO, 1.0);
[theImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
return theImage;
}

关于iOS Action 扩展如何获取小图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com