xcode UIImage 使用 contentsOfFile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29376520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImage using contentsOfFile
提问by rgamber
I am using
我在用
self.imageView.image = UIImage(named: "foo.png")
self.imageView.image = UIImage(named: "foo.png")
to select and load images in an UIIMageView
. I have the images in my app under the images.xcassets
. The problem is that this particular init
caches the image for reuse as per the official Apple documentation:
选择和加载UIIMageView
. 我的应用程序中的图像位于images.xcassets
. 问题在于,init
根据Apple 官方文档,此特定缓存图像以供重用:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system's cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
如果您有一个只显示一次的图像文件并希望确保它不会被添加到系统的缓存中,您应该使用imageWithContentsOfFile:创建图像。这将使您的一次性图像远离系统图像缓存,从而有可能改善应用程序的内存使用特性。
My view allows cycling through images before selecting one, and so the memory footprint goes on increasing as I cycle through and never goes down even when I navigate back from that view.
我的视图允许在选择图像之前循环浏览图像,因此内存占用会随着我循环浏览而增加,即使我从该视图返回时也不会减少。
So I am trying to use UIIMage(contentsOfFile: "path to the file")
which does not cache the image. Here I am having trouble programatically getting the path of the images which I have stored under images.xcassets.
所以我试图使用UIIMage(contentsOfFile: "path to the file")
哪个不缓存图像。在这里,我无法以编程方式获取我存储在images.xcassets下的图像的路径。
I have tried using:
我试过使用:
NSBundle.mainBundle().resourcePath
and NSBundle.mainBundle().pathForResource("foo", ofType: "png")
NSBundle.mainBundle().resourcePath
和 NSBundle.mainBundle().pathForResource("foo", ofType: "png")
without luck. For the first one I get the resourcePath, but on accessing that through the terminal I do not see any image assets under it, and the second one I get nil
when I use it. Is there an easy way to to do this?
没有运气。对于第一个,我获得了资源路径,但是在通过终端访问它时,我看不到它下面的任何图像资产,而第二个是nil
在我使用它时获得的。有没有一种简单的方法可以做到这一点?
Also looked at several SO questions (like this, thisand this) with no luck. Do I have to put my images somewhere else to be able to use the pathForResource()
? What is the right way to go about this?
还看了几个 SO 问题(如this,this和this),但没有运气。我是否必须将我的图像放在其他地方才能使用pathForResource()
?解决这个问题的正确方法是什么?
It is hard to imagine that no one has encountered this scenario before :) !
很难想象以前没有人遇到过这种情况:)!
回答by Kateryna Gridina
If you need to use pathForResource()for avoiding image caching, it is not possible to work with images.xcassets. In that case you need to create group in XCode, and add image there (be sure, that image is copied to Copy Bundle Resources). Afterwards just write:
如果您需要使用pathForResource()来避免图像缓存,则无法使用 images.xcassets。在这种情况下,您需要在 XCode 中创建组,并在那里添加图像(确保该图像被复制到复制捆绑资源)。之后只需写:
Swift 5:
斯威夫特 5:
let bundlePath = Bundle.main.path(forResource: "imageName", ofType: "jpg")
let image = UIImage(contentsOfFile: bundlePath!)
Older Swift:
年长的斯威夫特:
let bundlePath = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg")
let image = UIImage(contentsOfFile: bundlePath!)
回答by Mohammad Razipour
import Foundation
import UIKit
enum UIImageType: String {
case PNG = "png"
case JPG = "jpg"
case JPEG = "jpeg"
}
extension UIImage {
convenience init?(contentsOfFile name: String, ofType: UIImageType) {
guard let bundlePath = Bundle.main.path(forResource: name, ofType: ofType.rawValue) else {
return nil
}
self.init(contentsOfFile: bundlePath)!
}
}
Use:
用:
let imageView.image = UIImage(contentsOfFile: "Background", ofType: .JPG)
回答by Haileapp
When you implement array of images from the images group resource,you can load each images(lets say b1,b2,b3,b4,b5) as
当您从图像组资源中实现图像数组时,您可以将每个图像(比如 b1、b2、b3、b4、b5)加载为
var imageIndex = 0
lazy var imageList = ["b1","b2","b3","b4","b5"]
let imagePath = Bundle.main.path(forResource: imageList[imageIndex], ofType: "jpg")
imageview.image = UIImage(contentsOfFile: imagePath!)