菜鸟教程小白 发表于 2022-12-11 18:32:17

iOS Action 扩展如何获取小图片


                                            <p><p>在系统照片库中,打开我的 Action 扩展,因为图片太大,扩展崩溃了,如何才能得到一个更小的图片?</p>

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

<pre><code>NSDictionary * dict =@{NSItemProviderPreferredImageSizeKey:};
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:dict completionHandler:^(UIImage *image, NSError *error) {

}];
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 imageView 上加载图像之前。首先调整它的大小。然后在 imageView 上加载调整大小的图像。</p>

<pre><code>UIImage *img = ;
selectedImage = ;
</code></pre>

<p>调整图像数据大小:</p>

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

      if(theImage.size.width &gt; 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);
      ;
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
    }
    return theImage;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOSAction 扩展如何获取小图片,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45811752/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45811752/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS Action 扩展如何获取小图片