小编ndg*_*ndg的帖子

简单的圆形手势检测

我正在寻找一种简单的,以编程方式检测用户是否绘制了圆形的方法.我在C工作,但很高兴从伪代码工作.一些谷歌搜索带来了许多(希望)过于复杂的方法.

我正在跟踪鼠标坐标作为浮点数,并创建了一个矢量数组来跟踪鼠标随时间的移动.基本上我想要检测何时绘制一个圆,然后忽略与该圆无关的所有运动数据.

我基本了解如何实现这一目标:

使用轮询功能跟踪所有移动.每次轮询该功能时,都会存储当前鼠标位置.在这里,我们遍历历史位置数据并做一个粗略的"对齐位置"来比较两个位置.如果新位置距离旧位置足够近,我们会删除旧位置之前的所有历史数据.

虽然这在理论上有效,但在实践中却是一团糟.有没有人有什么建议?如果建议的方法可以检测它是顺时针还是逆时针绘制的加分点.

c gesture-recognition

15
推荐指数
2
解决办法
5373
查看次数

使用HTML5的画布使用外部笔划绘制文本

我目前正在使用HTML5的画布使用fillText方法渲染大量字符串.这很好,但我也想给每个字符串一个1px的黑色外笔画.不幸的是,strokeText函数似乎应用了内部笔划.为了解决这个问题,我编写了一个drawStrokedText函数来实现我所追求的效果.不幸的是它很慢(原因很明显).

是否有使用本机画布功能实现1px外部笔划的快速,跨浏览器方式?

drawStrokedText = function(context, text, x, y)
{
    context.fillStyle = "rgb(0,0,0)";
    context.fillText(text, x-1, y-1);
    context.fillText(text, x+1, y-1);
    context.fillText(text, x-1, y);
    context.fillText(text, x+1, y);
    context.fillText(text, x-1, y+1);
    context.fillText(text, x+1, y+1);

    context.fillStyle = "rgb(255,255,255)";
    context.fillText(text, x, y);
};
Run Code Online (Sandbox Code Playgroud)

这是工作效果的一个例子:

在此输入图像描述

javascript html5 canvas html5-canvas

14
推荐指数
4
解决办法
2万
查看次数

编写基本推荐引擎

我正在寻找一个基本的推荐引擎,它将获取并存储一个数字ID列表(与书籍相关),将这些ID与具有大量相同ID的其他用户进行比较,并根据这些查找推荐其他书籍.

经过一段谷歌搜索,我发现这篇文章讨论了Slope One算法的实现,但似乎依赖于用户评价被比较的项目.理想情况下,我希望在不需要用户提供评级的情况下实现这一目标.我假设如果用户在他们的收藏中有这本书,他们会喜欢它.

虽然我觉得我可以默认每本书的评级为10,但我想知道我是否可以使用更高效的算法.理想情况下,我想动态计算这些建议(避免批量计算).任何建议,将不胜感激.

recommendation-engine

12
推荐指数
1
解决办法
5202
查看次数

将NSTimer放在单独的线程中

注意:可能需要向下滚动才能阅读我的编辑内容.

我正在尝试在单独的线程中设置NSTimer,以便在用户与我的应用程序的UI交互时继续触发.这似乎有效,但Leaks报告了一些问题 - 我相信我已将其缩小到我的计时器代码.

目前正在发生的事情是updateTimer尝试访问NSArrayController(timersController),它绑定到我的应用程序界面中的NSTableView.从那里,我抓住第一个选定的行并更改其timeSpent列.注意:timersController的内容是通过Core Data生成的托管对象的集合.

通过阅读,我相信我应该尝试做的是在主线程上执行updateTimer函数,而不是在我的计时器辅助线程中.

我在这里发帖是希望有经验的人可以告诉我这是否是我唯一做错的事情.阅读了Apple关于线程的文档后,我发现它是一个非常庞大的主题领域.

NSThread *timerThread = [[[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil] autorelease];
[timerThread start];

-(void)startTimerThread
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    activeTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];

    [runLoop run];
    [pool release];
}
-(void)updateTimer:(NSTimer *)timer
{
    NSArray *selectedTimers = [timersController selectedObjects];
    id selectedTimer = [selectedTimers objectAtIndex:0];
    NSNumber *currentTimeSpent = [selectedTimer timeSpent];

    [selectedTimer setValue:[NSNumber numberWithInt:[currentTimeSpent intValue]+1] forKey:@"timeSpent"];
}
-(void)stopTimer
{
    [activeTimer invalidate];
    [activeTimer release];
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

关于这次泄漏,我仍然完全迷失了.我知道我显然做错了什么,但我已经将我的应用程序剥离到了它的骨头,似乎仍然无法找到它.为了简单起见,我已将我的应用程序控制器代码上传到: …

cocoa multithreading

8
推荐指数
1
解决办法
1万
查看次数

使用带有geometryFlipped的CALayer的renderInContext:方法

我有一个CALayer(containerLayer),我想在将NSBitmapImageRep数据保存为平面文件之前将其转换为a .containerLayer将其geometryFlipped属性设置为YES,这似乎导致了问题.最终生成的PNG文件正确呈现内容,但似乎不考虑翻转的几何体.我显然正在寻找test.png来准确地表示左边显示的内容.

下面是问题的截图和我正在使用的代码.

一个直观的例子

- (NSBitmapImageRep *)exportToImageRep
{
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    int bitmapByteCount;
    int bitmapBytesPerRow;

    int pixelsHigh = (int)[[self containerLayer] bounds].size.height;
    int pixelsWide = (int)[[self containerLayer] bounds].size.width;

    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    context = CGBitmapContextCreate (NULL,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context == NULL)
    {
        NSLog(@"Failed to create context.");
        return nil;
    }

    CGColorSpaceRelease(colorSpace);
    [[[self containerLayer] presentationLayer] renderInContext:context];    

    CGImageRef img = …
Run Code Online (Sandbox Code Playgroud)

cocoa core-graphics calayer nsbitmapimagerep

7
推荐指数
1
解决办法
2511
查看次数

HTML5画布图像缓存/ putImageData问题

我正在使用HTML5画布加载图像的单个实例,然后我将其多次blit到一个画布上.图像需要一些轻微的基于像素的操作才能对其进行自定义.我最初的攻击计划是加载图像,将其blit到支持画布,在其上绘制我的修改,然后抓取图像数据并将其缓存以备将来使用.

这是我写的一些代码:

context.drawImage(img, 0, 0);
context.fillStyle = '#ffffff';
context.fillRect(0, 0, 2, 2);  // Draw a 2x2 rectangle of white pixels on the top left of the image

imageData = context.getImageData(0, 0, img.width, img.height);
cusomImage = imageData;
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但我注意到我的图像(透明的PNG)不能保持透明度.相反,当使用putImageData将其放置在我的前置画布上时,它将以黑色背景呈现.我如何保持透明度?

欢迎任何建议!

javascript html5 canvas getimagedata

6
推荐指数
1
解决办法
8065
查看次数

使用EXC_BAD_ACCESS阻止回调崩溃

我有一个自定义的NSOperation子类,用于发出HTTP请求.它接受在NSOperation完成时执行的基于块的回调.一切都有效,但是在尝试执行完成回调时,我遇到了一个奇怪的,间歇性的崩溃.我读过很多基于块的EXEC_BAD_ACCESS问题是由于在将块传递给其他方法时没有正确复制块引起的.

我相信我的问题与我如何利用积木有关.我将在下面为我的应用程序提供一个标准用例.我的问题的根源可能归结为所有权误解了涉及块的地方.

// Perform a HTTP request to a specified endpoint and declare a callback block
[self performRequestToEndpoint:@"endpoint" completion:^(HTTPResponse *response) {
    NSLog(@"Completed with response: %@", response);
}];

// A helper function to avoid having to pass around too many parameters
- (void)performRequestWithEndpoint:(NSString *)endpoint completion:(void (^)(HTTPResponse *response))completionBlock
{
    // Make our HTTP request and callback our original completion block when done
    [self requestWithMethod:@"GET" path:endpoint completion:^(HTTPResponse *response) {
        if(![response error])
        {
            // Call our original completion block
            completionBlock(response);
        }
    ];
}
Run Code Online (Sandbox Code Playgroud)

当通过requestWithMethod:path:completion:方法分配回调块时,它被复制如下: …

cocoa objective-c objective-c-blocks

6
推荐指数
1
解决办法
2821
查看次数

Objective-C中的二维数组?

我正在开发一款需要单屏平面图的基本iPhone游戏.那里没什么难的.我来自C背景,所以我目前的解决方案看起来有点像这样:

typedef struct _Tile {
    NSString *type;
} Tile;

@interface Map {
    Tile mapData[MAP_TILE_MAX_X][MAP_TILE_MAX_Y];
}
Run Code Online (Sandbox Code Playgroud)

这很好,但我想知道是否有一种更"正确"的方式来处理Objective-C的事情.如果我采用Objective-C方法,我会看到情况如何:我将创建一个基本的Tile类来保存基本的tile属性,然后我可以为特定的tile类型(@interface Water : Tile {}例如)创建子类.这将允许我也具有特定于Tile的逻辑.例如:Tile类可以有一个'think'方法来执行任何必要的逻辑.在我的Water子类中,如果玩家被淹没,这可能会产生涟漪效应.

我的问题是:

  1. 在这种情况下使用C结构是否可以接受?如果没有,我的Obj-C方法是否正确?
  2. 如果我要创建一个基本Tile类并使用特定Tile类型的子类,我将如何动态实例化每个Tile子类(假设我有一个包含类型'water'的NSString,我需要实例化Water类).

iphone struct objective-c nsarray

5
推荐指数
1
解决办法
724
查看次数

在转换为NSString后释放CFStringRef

我正在转换一个Objective-C函数,它将CFStringRef转换为类似的NSString(其中FSEventStreamCopyDescription返回一个CFStringRef):

return (NSString *)FSEventStreamCopyDescription(eventStream);
Run Code Online (Sandbox Code Playgroud)

但是,在分析我的应用程序时,我被告知由于缺少CFRelease调用,这将导致"潜在泄漏".我正在尝试重写此函数以避免任何泄漏.处理这个问题的正确方法是什么?我提供了两种可能性(两者都可能不正确):

选项A:

CFStringRef ref = FSEventStreamCopyDescription(eventStream);
NSString *desc = [(NSString *)ref copy];
CFRelease(ref);

return [desc autorelease];
Run Code Online (Sandbox Code Playgroud)

选项B:

CFStringRef ref = FSEventStreamCopyDescription(eventStream);
NSString *desc = [[NSString alloc] initWithString:(NSString *)ref];
CFRelease(ref);

return [desc autorelease];
Run Code Online (Sandbox Code Playgroud)

memory cocoa memory-leaks objective-c

5
推荐指数
1
解决办法
5496
查看次数

使用NSPredicateEditor强制显示复合行

我正在代码中创建一个NSPredicateEditor,并试图实现我希望的相当简单的任务.基本上,我想显示一个复合NSPredicateEditorRowTemplate(给用户提供在"All/Any/None以下都是真"时执行匹配的选项)和它下面的一些相当基本的子行.为此,我构造我的NSPredicateEditor并绑定其值,以便在用户编辑其谓词时保存更改.

我遇到的问题是,无论如何,我都不能将复合NSPredicateEditorRowTemplate默认显示为具有单个子行的父行.当我最初创建谓词时,我传递了一个虚假的空谓词,如下所示:

filename BEGINSWITH[cd] ''
Run Code Online (Sandbox Code Playgroud)

为了强制显示复合行,我必须创建两个子行:

filename BEGINSWITH[cd] '' && path ==[cd] ''
Run Code Online (Sandbox Code Playgroud)

作为参考,这是我如何构建NSPredicateEditor:

NSArray *keyPaths = [NSArray arrayWithObjects:[NSExpression expressionForKeyPath:@"filename"], [NSExpression expressionForKeyPath:@"path"], nil];
NSArray *operators = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSEqualToPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSNotEqualToPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSBeginsWithPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSEndsWithPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSContainsPredicateOperatorType],
                                                                            nil];

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:keyPaths
                                                                          rightExpressionAttributeType:NSStringAttributeType
                                                                                              modifier:NSDirectPredicateModifier
                                                                                             operators:operators
                                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

NSArray *compoundTypes = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSNotPredicateType],
                                                        [NSNumber numberWithInteger:NSAndPredicateType],
                                                        [NSNumber numberWithInteger:NSOrPredicateType],
                                                        nil];

NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];
NSArray *rowTemplates = [NSArray arrayWithObjects:compound, template, nil];
[template release];
[compound release];

predicateEditor = …
Run Code Online (Sandbox Code Playgroud)

xcode cocoa nspredicateeditor nspredicate

5
推荐指数
1
解决办法
1104
查看次数