简体   繁体   English

iOS 负载缩放 UIImage 从资产

[英]iOS load scaled UIImage from assets

I'm implementing a react-native component for iOS and need to return a UIImage .我正在为 iOS 实现 react-native 组件,并且需要返回UIImage What I have is return [UIImage imageNamed:@"myAsset"];我所拥有的是return [UIImage imageNamed:@"myAsset"]; which is working but the image presented is way too small.这是有效的,但呈现的图像太小了。

How can I load an asset image and make it bigger?如何加载资产图像并使其更大?

Another aspect is, that the impl returning the UIImage is invoked quite often for objects we draw onto the screen, but they are all the same, so scaling with every call is maybe not a good idea, but I have no idea how to make assets having a size.另一方面是,对于我们在屏幕上绘制的对象,经常调用返回 UIImage 的 impl,但它们都是相同的,因此每次调用缩放可能不是一个好主意,但我不知道如何制作资产有大小。 Last but not least, it's a PDF asset.最后但同样重要的是,它是 PDF 资产。

What I've tried is this...我试过的是这个...

So I search for image scaling and came here:所以我搜索图像缩放并来到这里:

NSURL *url = [NSBundle.mainBundle URLForResource:@"myAsset" withExtension:nil];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data scale:0.5];
return image;

but now url is nil and it's not working.但现在url nil并且无法正常工作。

Then I found this:然后我发现了这个:

UIImage *image = [UIImage imageNamed:@"myAsset"];
NSData *rawData = (__bridge NSData *) CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));

return [UIImage imageWithData:rawData scale:0.5];

Which is somehow also not working at all.这在某种程度上也根本不起作用。

Now I hope maybe you can help me and thank you in advance.现在我希望你能帮助我并提前谢谢你。

func resizeImage(image: UIImage, newWidth: CGFloat, newHeight: CGFloat) -> UIImage {
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
    image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM