我正在尝试通过使用包含图像过滤器的单例对象来优化一些图像过滤,以供我在任何地方使用.我遇到了一个问题,我相信我一次多次调用过滤器(显示几个单元格,下载图像后,显示之前,我正在使用过滤器处理图像,这可能几乎同时发生) .
我遇到了这个问题:
NSAssert(framebufferReferenceCount > 0, @"Tried to overrelease a framebuffer, did you forget to call -useNextFrameForImageCapture before using -imageFromCurrentFramebuffer?");
https://github.com/BradLarson/GPUImage/blob/master/framework/Source/GPUImageFramebuffer.m#L269
这是我的代码:
接口:
#import <GPUImage/GPUImage.h>
@interface GPUImageFilterManager : NSObject
+ (GPUImageFilterManager*)sharedInstance;
@property (nonatomic, strong) GPUImageiOSBlurFilter *blurFilter;
@property (nonatomic, strong) GPUImageLuminanceThresholdFilter *luminanceFilter;
@end
Run Code Online (Sandbox Code Playgroud)
执行:
#import "GPUImageFilterManager.h"
@implementation GPUImageFilterManager
+ (GPUImageFilterManager*)sharedInstance {
static GPUImageFilterManager *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [GPUImageFilterManager new];
});
return _sharedInstance;
}
- (instancetype)init {
self = [super init];
if (self) {
self.luminanceFilter = …Run Code Online (Sandbox Code Playgroud) 我目前在从ViewController转移到PFFile的子类时数据丢失时遇到了一些麻烦.传递的数据是要上载到用户配置文件的图像数据.这是选择图像的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Dismiss controller
[picker dismissViewControllerAnimated:YES completion:nil];
// Resize image
_focusedImage.image = image;
NSData *imageData = UIImageJPEGRepresentation(image, 0.05f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];
[[imageUpload uploadImage] setImagePFFile:imageFile];
}
Run Code Online (Sandbox Code Playgroud)
imageFile此视图中的登录正确打印出来.但是,当我将数据传递给我的单例类时,imageUpload uploadImage这就是数据结构的样子:
+ (imageUpload *) uploadImage
{
static imageUpload*_sharedImageUpload = nil;
_sharedImageUpload = [[self alloc] init];
_sharedImageUpload.imageData = [[NSData alloc] init];
PFUser *user = [PFUser currentUser];
_sharedImageUpload.imagePFFile = [[PFFile alloc] …Run Code Online (Sandbox Code Playgroud) 我有一个返回单例的方法.
// Window.h
static Window &getSingleton();
// Window.cpp
Window &Window::getSingleton()
{
static Window singleton;
return singleton;
}
Run Code Online (Sandbox Code Playgroud)
例如,Objective-C我习惯于将单例分配给局部范围变量名.
UIApplication *application = [UIApplication sharedApplication];
Run Code Online (Sandbox Code Playgroud)
但是做类似的事情C++会导致创建一个新对象.
Window window = Window::getSingleton();
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为在这种情况下调用了复制构造函数Window(Window const &windowCopy).
有没有办法解决这个问题,或者当我需要与它进行交互时,我是否必须始终调用我的单例方法?
我需要将以下Objective-C代码转换为Swift.
static RWBasicCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
});
Run Code Online (Sandbox Code Playgroud)
我怎么做?我用Google搜索并找到了这个例子.
class SingletonC {
class var sharedInstance : SingletonC {
struct Static {
static var onceToken : dispatch_once_t = 0
static var instance : SingletonC? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = SingletonC()
}
return Static.instance!
}
}
Run Code Online (Sandbox Code Playgroud)
但这是为了返回一个班级.
我在这里提到了Jon Skeet的文章(http://csharpindepth.com/articles/general/singleton.aspx),第六个版本.
但是,我有一些私有变量,我想初始化一次,并被这个所谓的单例类中的方法使用.我在私有构造函数中初始化它们,但很快发现,在多线程场景(Task.Run)中调用方法时它们是null .
在调试时,我观察到私有构造函数在调用"实例"时没有调用两次(应该是),因此我假设我的私有变量在那个时间点不应该为空(成功的"实例"调用).
关于如何声明,初始化和使用这些变量的任何想法?
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
// my private variables
private readonly string _plantCode;
private Singleton()
{
var appSettings = ConfigurationManager.AppSettings;
string _plantCode = appSettings["PlantCode"] ?? "Not Found";
}
public SomeMethod()
{
var temp = _plantCode; // <== _plantCode becomes null here!
}
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个Application Singleton,用于在活动之间共享数据.
AppData (Singleton)
import android.app.Application;
public class AppData extends Application {
private String data;
public String getData(){ return this.data;}
public void setData(String data){this.data = data; }
}
Run Code Online (Sandbox Code Playgroud)
主要活动
public class SectionListExampleActivity extends Activity {
AppData appData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_list_layout);
appData = (AppData) getApplicationContext(); // error here
}
Run Code Online (Sandbox Code Playgroud)
错误: android.app.Application cannot be cast to com.example....AppData
考虑Singleton:
public final class MySingleton {
private static class Nested
{
private static final MySingleton INSTANCE = new MySingleton();
}
private MySingleton()
{
if (Nested.INSTANCE != null)
{
throw new IllegalStateException("Already instantiated");
}
}
public static MySingleton getInstance()
{
return Nested.INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有放任何锁,但为什么这是Singleton问题的线程安全解决方案?
所以这真令人沮丧。我正在尝试使用单例传递字符串变量,但它没有在第二个UIViewController上传递。
Singleton.swift:
class CardName {
var cardName : String = ""
class var sharedInstance : CardName {
struct Static {
static let instance : CardName = CardName()
}
return Static.instance
}
var returnedCardName : String {
get{
return self.cardName
}
set {
self.cardName = newValue
}
}}
Run Code Online (Sandbox Code Playgroud)
查看1:
CardName.sharedInstance.returnedCardName = "Single pass test"
print("Shared instance name set: \(CardName.sharedInstance.returnedCardName)")
Run Code Online (Sandbox Code Playgroud)
print()成功显示了正确设置的字符串。
查看2:
let card_name_shared = CardName.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
card_Name = card_name_shared.returnedCardName
print("Passed Card Name: \(self.card_name_shared.returnedCardName)")
}
Run Code Online (Sandbox Code Playgroud)
查看2个print()结果: …
我试图理解单例设计模式并创建了一个最简单的模式:
#include <iostream>
class mySingleton{
private:
static mySingleton *ptr;
mySingleton(){ }
public:
static mySingleton* getInstance(){
if(!ptr){
ptr = new mySingleton();
return ptr;
} else return ptr;
}
void msg(){
std::cout << " Hello World!! " << std::endl;
}
};
int main(){
mySingleton* obj = mySingleton::getInstance();
mySingleton* obj2 = mySingleton::getInstance();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,我得到:
Undefined symbols for architecture x86_64:
"mySingleton::ptr", referenced from:
mySingleton::getInstance() in ccm822LI.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么我不能在静态函数中使用ptr,因为ptr也是一个静态变量?我在这里错过了什么吗?
我阅读了有关scala中单例对象的文章,但没有找到关于它是否是类的实例的内容.以下简单程序告诉我,对于这种特殊情况,它是真的:
class TestMatch(val i: Int)
object TestMatch{
def apply(i: Int) = new TestMatch(i)
def unapply(tm : TestMatch): Option[Int] = Some(tm.i)
}
Run Code Online (Sandbox Code Playgroud)
我试着像这样测试它:
println(TestMatch.isInstanceOf[TestMatch]) //false
Run Code Online (Sandbox Code Playgroud)
但它产生了警告fruitless type test.我不确定如何进行这样的测试.