iOS로 라이선스에서 이미지로드
내 프로젝트에 .bundle
이미지로 추가 폴더를 추가했습니다 . 이 이미지를 직접 참조하여 다음과 같이 작성하는 것이 맞 맞습니까? :
[UIImage imageNamed:@"imageInBundle.png"];
이러한 이미지에 액세스하고 사용하는 가장 좋은 방법은 무엇입니까?
맞습니다 imageNamed:
. 기본 제공을 검색합니다. 프로젝트 네비게이터의 다른 그룹에 있더라도 프로젝트의 이미지는 기본 할 수 있고 이름으로 직접 액세스 할 수 있습니다.
그래도 작동하지
[UIImage imageNamed:@"yourbundlefile.bundle/imageInBundle.png"];
베스트
1) 라이선스로 작업하는 경우 많은 대상으로 이동하여 COMBINE_HIDPI_IMAGES를 NO로 설정하십시오. 다른 경우에는 이미지가 tiff 형식으로 변환됩니다.
2) 다음 코드를 시도하십시오.
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"YourBundle" withExtension:@"bundle"]];
NSString *imagePath = [bundle pathForResource:@"imageInBundle" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
애플은 고맙게도, 아이폰 OS 8 이에 대한 적절한 API를 제공하고있다 imageNamed:inBundle:compatibleWithTraitCollection:
.
다음과 같이 사용됩니다.
[UIImage imageNamed:@"image-name"
inBundle:[NSBundle bundleForClass:self.class]
compatibleWithTraitCollection:nil]
또는 신속하게 :
let image = UIImage(named: "image-name",
inBundle: NSBundle(forClass: self),
compatibleWithTraitCollection: nil)
이 방법은 xcode 프로젝트의 모든 위치에서 이미지를 반환합니다.
+(UIImage*)returnImageFromResourceBundle:(NSString*)nameOfImage
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Resource" ofType:@"bundle"];
NSString *imageString = [[NSBundle bundleWithPath:bundlePath] pathForResource:nameOfImage ofType:@"png"];
UIImage *retrievedImage = [[UIImage alloc] initWithContentsOfFile: imageString];
return retrievedImage;
}
스위프트 3
let myImage = UIImage(named: "nameOfImage", in: Bundle(for: type(of: self)), compatibleWith: nil)
현재 성능을 입증 할 수 있습니다.
Bundle(for: type(of: self))
스위프트를 위해 보유 알아 내야 찍기 때문에 추가 할 생각했습니다 ....
if let imagePath = NSBundle.mainBundle().pathForImageResource("TestImage.png")
{
imageView.image = NSImage(contentsOfFile: imagePath)
}
내 많은 내 그룹 TestImage.png
에있는 곳 Supporting Files
.
Swift 3의 경우
let prettyImage = UIImage(named: "prettyImage",
in: Bundle(for: self),
compatibleWith: nil)
Swift 버전
@AndrewMackenzie의 답변이 작동하지 않습니다. 이것이 작동 한 것입니다
let image = UIImage(named: "imageInBundle.png")
imageView.image = image
내 전체 답변은 여기에 있습니다 .
동일한 viewController에서 두 번 사용하려는 경우 빠른 방법 :
아래 메소드와 동일한 클래스의 어느 위치에서나 이미지를 가져옵니다.
// this fetches the image from: MyBundle.bundle/folder/to/images/myImage.png
UIImage *myImage = [self imageFromBundle:@"MyBundle" withPath:@"folder/to/images/myImage.png"];
이미지를 가져 오는 방법 :
- (UIImage *)imageFromBundle:(NSString *)bundleName withPath:(NSString *)imageName
{
NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
NSString *imagePath = [bundle pathForResource:imageName ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
return image;
}
또는 서명 한 바로 호출하십시오.
UIImage *myImage = [UIImage imageNamed:@"MyBundle.bundle/folder/to/images/myImage.png"];
참고 URL : https://stackoverflow.com/questions/5664787/load-image-from-bundle-with-ios
'ProgramingTip' 카테고리의 다른 글
C #에서 이중 값 비교 (0) | 2020.12.06 |
---|---|
생성기 / 반복자에서 항목 수를 계산하는 가장 짧은 방법은 무엇입니까? (0) | 2020.12.06 |
쉘에서 마지막으로 캐치 작성 시도 (0) | 2020.12.06 |
SQL 서버에서 저장 프로 시저의 예약 된 실행 (0) | 2020.12.06 |
DLL에 의해 노출 된 모든 기능을 찾는 방법이 있습니까? (0) | 2020.12.06 |