iOS操作扩展如何获取小图像
在系统照片库中,打开我的动作分机,因为图像太大,分机崩溃了,我怎样才能得到更小的图像 我尝试使用下面的代码,但它不起作用iOS操作扩展如何获取小图像,ios,objective-c,Ios,Objective C,在系统照片库中,打开我的动作分机,因为图像太大,分机崩溃了,我怎样才能得到更小的图像 我尝试使用下面的代码,但它不起作用 NSDictionary * dict =@{NSItemProviderPreferredImageSizeKey:[NSValue valueWithCGSize:CGSizeMake(300, 300)]}; [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:dict com
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;
}
-(UIImage*)调整图像大小:(UIImage*)图像
{
如果((图像大小宽度>1024和图像大小高度>768)|(图像大小宽度>768和图像大小高度>1024)|(图像大小宽度>1024和图像大小高度<768)|(图像大小高度>1024和图像大小高度<768))
{
浮动newHeight、newWidth、oldWidth、oldHeight、scaleFactor;
如果(图像大小宽度>图像大小高度)
{
oldWidth=image.size.width;
scaleFactor=1024/oldWidth;
newHeight=image.size.height*缩放因子;
newWidth=oldWidth*scaleFactor;
}
其他的
{
oldHeight=图像大小高度;
scaleFactor=1024/oldHeight;
newWidth=image.size.width*缩放因子;
新高度=旧高度*缩放因子;
}
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth,newHeight),NO,1.0);
[TheImageDrawinRect:CGRectMake(0,0,新宽度,新高度)];
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回新图像;
}
返回图像;
}
选择图像后检查图像大小,如果需要,请减小图像大小,这样可能会有帮助。在上述情况下,如果您猜测这是问题所在,则加载图像时会崩溃,因此没有机会减小图像大小。因此,它必须得到一个较小的图像加载时…这将是内存过载之前的图像加载,所以应该有一个方法来加载一个较小的图像之前,行动扩展启动。