English 中文(简体)
在试图将UIImage编码和储存到NSUserdefault时,申请变得缓慢。
原标题:App becomes slow when trying to encode UIImage and storing to NSUserdefault

我正试图在用户点击我方言中某个表格时,把用户名单(拥有UIImage财产)留给NSUserdefault。

在我的用户目标中,我实施了如下编码:

- (void)encodeWithCoder:(NSCoder *)encoder
{
    NSURL *url = [NSURL URLWithString:self.avatar_url];
    NSData *data = [NSData dataWithContentsOfURL:url];
    self.avatar = [[[UIImage alloc] initWithData:data] autorelease];

    [encoder encodeObject:UIImagePNGRepresentation(self.avatar) forKey:@"avatar"];
}

然而,我认识到,通过这样做,我在点击触发拯救NSUserdefault的表格后hang。 我将缓慢的表现归因于从URL中转换(和编码)。

是否有办法加快这个过程,把图像保存到NSUserdefault(这不会导致电 app滞)。

最佳回答

其原因是,在图像下载之前,你阻止了主线。 每当依靠网络活动开展业务活动时,就应当把这种活动移至背景。 而正如“Richard J. Rosss III”指出,你可以直接节省数据,除非你绝对需要一个P纳克,而原来的格式并不平。

rel=“nofollow> 应急计划指南

问题回答

如果URL是遥远的(而不是地方file http:///URL),那么,履约问题很可能是下载的问题——研究如何使用,以及相关类别以下载ava asynchously(背景),然后将其储存到NSUserDefaults

并请注意,录制的图像并非打算使用,请在您的文件目录中填写:

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"avatar"];
[data writeToFile:path atomically:YES];

并且要说:

NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:data];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签