for best ios iphone objective-c xcode cocoapods

ios - best - sdwebimage



Cargar imagen desde el paquete de recursos de CocoaPods (6)

Estoy creando una biblioteca privada que incluye recursos a los que hacen referencia los componentes de la biblioteca. La biblioteca se comparte con aplicaciones usando CocoaPods. En el .podspec para la biblioteca, he incluido una línea como esta:

s.resource_bundles = {''IXMapKitResources'' => [''IXMapKit/Resources/*'']}

Uno de los recursos en el paquete es un catálogo de activos con varios conjuntos de imágenes, uno de los cuales se llama ''IXMKAnnotationIcons-Normal-Accident''. El siguiente código devuelve un nulo:

UIImage * image = [UIImage imageNamed: @"IXMKAnnotationIcons-Normal-Accident"];

Encontré un artículo en mokacoding.com que describe cómo cargar fuentes desde un pod, pero esto no me funcionó:

- (UIImage *) ixmk_imageNamed: (NSString *) name { NSString * IXMKResourceBundleName = @"IXMapKitResources.bundle"; NSString * resourceName = [NSString stringWithFormat: @"%@/%@", IXMKResourceBundleName, name]; NSString * imageTypeString = [self ixmk_stringForImageType: imageType]; NSURL * url = [[NSBundle mainBundle] URLForResource: resourceName withExtension: imageTypeString]; NSData * imageData = [NSData dataWithContentsOfURL: url]; if (imageData != nil) { CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) imageData); CGImageRef imageRef = [self ixmk_imageFromDataProvider: provider imageType: imageType]; UIImage * image = [UIImage imageWithCGImage: imageRef]; CFRelease(imageRef); CFRelease(provider); return image; } else { return nil; } }

La página web de CocoaPods que describe la palabra clave de recursos tiene la siguiente advertencia:

Recomendamos encarecidamente a los desarrolladores de bibliotecas que adopten paquetes de recursos ya que puede haber colisiones de nombres usando el atributo de recursos. Además, los recursos especificados con este atributo se copian directamente en el objetivo del cliente y, por lo tanto, no están optimizados por Xcode.

Estoy en una pérdida de qué hacer aquí.


En cuanto a mí, tuve que agregar "s.resources" en .podspec. Antes de que esto faltara.

Solo los s.resource_bundles se configuraron de esta manera: ''BPPicker / Assets / . '', lo cambió a'' BPPicker / Assets / * ''también.

# # Be sure to run `pod lib lint BPPicker.podspec'' to ensure this is a # valid spec before submitting. # # Any lines starting with a # are optional, but their use is encouraged # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html # Pod::Spec.new do |s| s.name = ''BPPicker'' s.version = ''0.1.11'' s.summary = ''This is the imito Body Part Picker.'' # This description is used to generate tags and improve search results. # * Think: What does it do? Why did you write it? What is the focus? # * Try to keep it short, snappy and to the point. # * Write the description between the DESC delimiters below. # * Finally, don''t worry about the indent, CocoaPods strips it! s.description = <<-DESC TODO: Add long description of the pod here. DESC s.homepage = ''https://bitbucket.org/imito/bppicker'' # s.screenshots = ''www.example.com/screenshots_1'', ''www.example.com/screenshots_2'' s.license = { :type => ''MIT'', :file => ''LICENSE'' } s.author = { ''Ievgen Naloiko'' => ''[email protected]'' } s.source = { :git => ''https://[email protected]/imito/bppicker.git'', :tag => s.version.to_s } # s.social_media_url = ''https://twitter.com/<TWITTER_USERNAME>'' s.ios.deployment_target = ''9.0'' s.source_files = ''BPPicker/Classes/**/*'' s.resource_bundles = { ''BPPicker'' => [''BPPicker/Assets/*''] } s.resources = "BPPicker/**/*.{png,json}" s.frameworks = ''UIKit'' s.dependency ''ObjectMapper'' end


Esta respuesta funciona con Swift3 y Swift4 .

Crear una función en el directorio pod en el archivo.

func loadImageBundle(named imageName:String) -> UIImage? { let podBundle = Bundle(for: self.classForCoder) if let bundleURL = podBundle.url(forResource: "Update with directory name", withExtension: "bundle") { let imageBundel = Bundle(url:bundleURL ) let image = UIImage(named: imageName, in: imageBundel, compatibleWith: nil) return image } return nil }

Uso

imageView.image = loadImageBundle(named: "imagefile")


Esto resulta ser un problema con los catálogos de activos, no con CocoaPods o paquetes. Mover las imágenes del catálogo de activos solucionó el problema. Parece que Apple no admite catálogos de activos en paquetes de recursos secundarios. Lástima.


Hice un pod que puede ubicar los paquetes de recursos en los pods. Espero que termine el problema del "paquete de recursos de CocoaPods" de una vez por todas.

https://github.com/haifengkao/PodAsset



Por ejemplo Paramount

podspec

s.resource_bundle = { ''Paramount'' => [''Sources/Paramount.bundle/*.png''] }

swift

public extension UIImage { static func make(name: String) -> UIImage? { let bundle = NSBundle(forClass: Paramount.Toolbar.self) return UIImage(named: "Paramount.bundle//(name)", inBundle: bundle, compatibleWithTraitCollection: nil) } }

Aquí Paramount.Toolbar.self puede ser cualquier clase en ese marco