小编Ian*_*ell的帖子

如何在iPhone上动态创建彩色1x1 UIImage?

我想基于UIColor动态创建1x1 UIImage.

我怀疑这可以通过Quartz2d快速完成,而我正在仔细研究文档,试图掌握基础知识.但是,看起来存在很多潜在的缺陷:没有正确识别每个事物的位数和字节数,没有指定正确的标志,没有释放未使用的数据等.

如何使用Quartz 2d(或其他更简单的方法)安全地完成这项工作?

core-graphics quartz-graphics uiimage ios

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

Git钩子可以自动添加文件到提交吗?

我想使用Git中的预提交或后提交挂钩将自动生成的文件添加到同一提交中,这取决于在该提交中修改的文件.我该怎么做?

我已经尝试过这个作为预先提交的钩子,但没有运气:

#!/bin/sh
files=`git diff --cached --name-status`
re="<files of importance>"
if [[ $files =~ $re ]]
then
  echo "Creating files"
  exec bundle exec create_my_files
  exec git add my_files
  exec git commit --amend -C HEAD
fi
Run Code Online (Sandbox Code Playgroud)

这会成功将它们添加到存储库,但不会将它们添加到提交中.我也尝试在post-commit钩子中使用最后两个exec行以及pre-commit检查,但也没有好处.

git pre-commit pre-commit-hook githooks

58
推荐指数
5
解决办法
2万
查看次数

不引人注目的Javascript富文本编辑器?

我们已经使用不再支持的RichTextBox控件作为我们(基于ASP.NET的)CMS的一部分很长一段时间了,我们想用更轻量级和更好的跨浏览器支持替换它.我们最初看的是各种ASP.NET组件,但我想知道我们是否会更好地使用开源的全Javascript解决方案.

我是最近转换为jQuery的,我一直很惊讶于纯粹在客户端可以使用非常紧凑的附加组件(如Flexigrid),当然还有优秀的WMD.我已经为所有Javascript编辑做了一点点,这是我到目前为止所发现的:

经过肤浅的回顾,Tiny MCE看起来是个不错的选择; 但我有兴趣听到SO社区中实际使用过这些的人.让我知道你的想法.

javascript asp.net jquery

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

Android位图限制 - 防止java.lang.OutOfMemory

我目前正在努力解决Android平台的奇怪行为 - Bitmap/Java堆内存限制.根据设备的不同,Android会将应用程序开发人员限制为16,24或32 MiB的Java堆空间(或者您可能会在有根电话上找到任意随机值).这可以说是相当小,但相对简单,因为我可以使用以下API测量使用情况:

Runtime rt = Runtime.getRuntime();
long javaBytes = rt.totalMemory() - rt.freeMemory();
long javaLimit = rt.maxMemory();
Run Code Online (Sandbox Code Playgroud)

很容易; 现在为了扭曲.在Android中,除少数例外的位图存储在本机堆中,不计入Java堆.谷歌的一些眼光炯炯,纯粹的开发人员认为这是"糟糕的",并允许开发者获得"超过他们的公平份额".因此,有一个很好的小代码可以计算位图和可能的其他资源所产生的本机内存使用量,并与Java堆相加,如果你去了... java.lang.OutOfMemory. 哎哟

但没什么大不了的.我有很多位图,并且不需要所有这些位图.我可以"分页"一些目前尚未使用的内容:

因此,对于尝试#1,我重构了代码,因此我可以使用try/catch包装每个位图加载:

while(true) {
    try {
        return BitmapFactory.decodeResource(context.getResources(), android_id, bitmapFactoryOptions);
    } catch (java.lang.OutOfMemory e) {
        // Do some logging

        // Now free some space (the code below is a simplified version of the real thing)
        Bitmap victim = selectVictim();
        victim.recycle();
        System.gc(); // REQUIRED; else, weird behavior ensues
    }
}
Run Code Online (Sandbox Code Playgroud)

看,这是一个很好的小日志片段,显示我的代码捕获异常,并回收一些位图:

E/Epic    (23221): OUT_OF_MEMORY (caught java.lang.OutOfMemory)
I/Epic    (23221): ArchPlatform[android].logStats() …

android out-of-memory

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

具有动态单元格高度的UITableView - 我需要做些什么来修复向下滚动?

我正在iPhone上建立一个小小的Twitter小客户端.当然,我在UITableView中显示推文,它们当然有不同的长度.我根据文字动态地改变单元格的高度:

- (CGFloat)heightForTweetCellWithString:(NSString *)text {
  CGFloat height = Buffer + [text sizeWithFont:Font constrainedToSize:Size lineBreakMode:LineBreakMode].height;
  return MAX(height, MinHeight);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *text = // get tweet text for this indexpath
    return [self heightForTweetCellWithString:text];
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用PragProg书中的算法显示实际的推文单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"TweetCell";
  TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [self createNewTweetCellFromNib];
  }
  cell.tweet.text = // tweet text
  // set other labels, etc
  return cell; …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview

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

我正在使用ARC在Objective-C中编写一个Button类 - 如何在选择器上阻止Clang的内存泄漏警告?

我正在编写一个简单的按钮类,如下所示:

@interface MyButton : NSObject {
  id object;
  SEL action;
}
@property(strong) id object;
@property SEL action;
-(void)fire;
@end


@implementation MyButton

@synthesize object, action;

-(void)fire {
  [object performSelector:action];
}

@end
Run Code Online (Sandbox Code Playgroud)

我从Clang得到以下警告[object performSelector:action]:

PerformSelector may cause a leak because its selector is unknown
Run Code Online (Sandbox Code Playgroud)

经过一些研究后,我发现选择器可以属于具有不同内存要求的系列.目的是使行动失效,因此不应引起任何ARC困难并且应该适合none家庭.

它看起来像我想要的相关预处理器代码片段,或者是以下的变体:

__attribute__((objc_method_family(none)))
Run Code Online (Sandbox Code Playgroud)

但是我在哪里告诉Clang不要担心?

objective-c clang automatic-ref-counting

19
推荐指数
3
解决办法
4593
查看次数

如何使用JQuery AJAX使用HTTP Basic Auth阻止Firefox提示用户名/密码?

我正在编写一些浏览器端动态功能,并使用HTTP Basic Auth来保护一些资源.用户体验非常重要,并且高度定制.

这是一个简单的测试JQuery方法,最终将测试用户是否在表单中提供了正确的凭据:

$(document).ready(function() {
    $("#submit").click(function() {
    var token = Base64.encode($('#username').val() + ':' + $('#password').val());        
    $.ajax({
      url: '/private',
      method: 'GET',
      async: false,
      beforeSend: function(req) {
        req.setRequestHeader('Authorization', 'test:password');
      },
      error: function(request, textStatus, error) {
        if (request.status == 401) {
          alert('401');
        }
      }
    });
    return false;
  });
});
Run Code Online (Sandbox Code Playgroud)

如果不允许他们访问/private,那么他们应该只看到警告框.但是,在Firefox上,会弹出一个浏览器提供的登录表单(使用新凭据重试).Safari不会这样做.

我们希望通过自定义表单,淡入淡出,转换等完全控制体验.如何防止Firefox的默认框显示?(如果我们测试IE时会出现这个问题,我也很乐意听到那里的解决方案.)

javascript firefox jquery http-authentication

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

如何检查NSData blob是否有效作为NSURLSessionDownloadTask的resumeData?

我有一个使用新NSURLSessionAPI 下载后台的应用程序.当下载以NSURLSessionDownloadTaskResumeData提供的方式取消或失败时,我存储数据blob以便以后可以恢复.我注意到野外崩溃的时间非常少:

Fatal Exception: NSInvalidArgumentException
Invalid resume data for background download. Background downloads must use http or https and must download to an accessible file.
Run Code Online (Sandbox Code Playgroud)

这里出现的错误,这里resumeDataNSDatablob和session是一个实例NSURLSession:

if (resumeData) {
    downloadTask = [session downloadTaskWithResumeData:resumeData];
    ...
Run Code Online (Sandbox Code Playgroud)

数据由Apple API提供,序列化,然后在以后进行反序列化.它可能已损坏,但它永远不会为零(如if语句检查).

如何提前检查resumeData无效,以免我让应用程序崩溃?

ios ios7 nsurlsession nsurlsessiondownloadtask

10
推荐指数
1
解决办法
4122
查看次数

如何在Core Graphics中生成RGBA图像?

我正在尝试从文本生成RGBA8图像以用作OpenGL ES 2.0纹理.

+(UIImage *)imageFromText:(NSString *)text
{
  UIFont *font = [UIFont systemFontOfSize:20.0];  
  CGSize size  = [text sizeWithFont:font];

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef contextRef =  CGBitmapContextCreate (NULL,
                                                    size.width, size.height,
                                                    8, 4*size.width,
                                                    colorSpace,
                                                    kCGImageAlphaLast
                                                    );
  CGColorSpaceRelease(colorSpace);
  UIGraphicsPushContext(contextRef);

  [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsPopContext();

  return image;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有CGColorSpaceCreateDeviceRGBA,CGColorSpaceCreateDeviceRGB导致以下错误:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 448 bytes/row.
Run Code Online (Sandbox Code Playgroud)

我缺少什么来创建OpenGL想要的正确RGBA8格式?

更新:我更改了CGBitmapContextCreatefrom 的最后一个参数kCGImageAlphaNone(当我复制粘贴代码时)kCGImageAlphaLast,这是我错误尝试的几种变体之一.

更新2:UIGraphicsGetImageFromCurrentImageContext() …

iphone opengl-es core-graphics objective-c

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

如何使用类似宏的元编程方法扩展Ruby模块?

考虑以下扩展(多年来由多个Rails插件推广的模式):

module Extension
  def self.included(recipient)
    recipient.extend ClassMethods
    recipient.send :include, InstanceMethods
  end

  module ClassMethods
    def macro_method
      puts "Called macro_method within #{self.name}"
    end
  end

  module InstanceMethods
    def instance_method
      puts "Called instance_method within #{self.object_id}"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

如果您希望将其公开给每个班级,您可以执行以下操作:

Object.send :include, Extension
Run Code Online (Sandbox Code Playgroud)

现在您可以定义任何类并使用宏方法:

class FooClass
  macro_method
end
#=> Called macro_method within FooClass
Run Code Online (Sandbox Code Playgroud)

实例可以使用实例方法:

FooClass.new.instance_method
#=> Called instance_method within 2148182320
Run Code Online (Sandbox Code Playgroud)

但即使如此Module.is_a?(Object),您也无法在模块中使用宏方法.

module FooModule
  macro_method
end
#=> undefined local variable or method `macro_method' for FooModule:Module (NameError)
Run Code Online (Sandbox Code Playgroud)

这是真实的,即使你明确地包括原ExtensionModuleModule.send(:include, Extension). …

ruby module metaprogramming

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