为 xcode 中的所有 类 创建全局图像
Make Image Global for all classes in xcode
所以我使用 Facebook 图 url 创建了一个 jpeg 文件,用户 ID 在 ViewControllerA 中。此图片保存为 profilePicture。我的问题是如何使这个图像 (UIImage) 全局化?
换句话说,我只是想让这个图像对我制作的任何 ViewController 或其他类型的 class 可用,在 ViewControllerA 之外。我不确定将它设为全局还是 public 更合适,我只需要最好的方法让它可以从任何控制器访问。感谢您的帮助!
您可以在 AppDelegate class 中声明一个 属性
在Objective C中:
#import <UIKit/UIKit.h>
@property(nonatomic,strong)UIImage *yourImage;
并像
一样访问它
[(AppDelegate *)[[UIApplication sharedApplication] delegate] yourImage]
在Swift >=1.2:
import UIKit
var UIImage *yourImage
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.yourImage
在Swift < 1.2
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.yourImage
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ViewControllerB *vcb = segue.destinationViewController;
vcb.Bpic = profPicture;
}
设置一个
@property (nonatomic, retain) UIImage *pic;
在 ViewControllerA 中。在B中设置一个这样的属性,然后设置背景为profPicture。
这是让它从 1 VC 到另一个的简单方法。将此应用到您需要的任何地方。希望这可以帮助!
所以我使用 Facebook 图 url 创建了一个 jpeg 文件,用户 ID 在 ViewControllerA 中。此图片保存为 profilePicture。我的问题是如何使这个图像 (UIImage) 全局化?
换句话说,我只是想让这个图像对我制作的任何 ViewController 或其他类型的 class 可用,在 ViewControllerA 之外。我不确定将它设为全局还是 public 更合适,我只需要最好的方法让它可以从任何控制器访问。感谢您的帮助!
您可以在 AppDelegate class 中声明一个 属性
在Objective C中:
#import <UIKit/UIKit.h>
@property(nonatomic,strong)UIImage *yourImage;
并像
一样访问它[(AppDelegate *)[[UIApplication sharedApplication] delegate] yourImage]
在Swift >=1.2:
import UIKit
var UIImage *yourImage
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.yourImage
在Swift < 1.2
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.yourImage
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ViewControllerB *vcb = segue.destinationViewController;
vcb.Bpic = profPicture;
}
设置一个
@property (nonatomic, retain) UIImage *pic;
在 ViewControllerA 中。在B中设置一个这样的属性,然后设置背景为profPicture。
这是让它从 1 VC 到另一个的简单方法。将此应用到您需要的任何地方。希望这可以帮助!