簡體   English   中英

iOS動作擴展如何獲取小圖像

[英]iOS action extension how to get a small image

在系統圖片庫中,打開我的動作擴展程序,因為圖像太大,擴展程序崩潰了,我如何獲得較小的圖像?

我嘗試使用以下代碼,但無法正常工作。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM