Attach to Process > XXX.">

Restore the connection to "“XXX”的 iPod" and run "XXX" again, or if "XXX" is still running, you can attach to it by selecting Debug > Attach to Process > XXX.

这个要记一下,在写列表型的界面时.期中有不少的图片加载,因为要动态计算cell的高度,图片的个数,所以我直接就把我以前写过的类似的代码拿过来,成功加载后出现了崩溃,却没有任何bug,提示,只说Restore the connection to "“XXX”的 iPod" and run "XXX" again, or if "XXX" is still running, you can attach to it by selecting Debug > Attach to Process > XXX.,这种情况应该是内存泄漏或者加载图片过大,我就看了下图片确实挺大的,近2M,可是怎么办呢,囧,

想起我以前做过的处理我是过滤每张图片,将每个图片都压缩成固定大小,太小查看时就很模糊了,用户体验很不好,代码也找不到了,于是去看别人怎么做的,发现了一个挺不错的记录下来

下面是他写的


SDWebImage大家肯定都恨熟悉了,国内外太多的App使用其进行图片加载。

但是最近在使用过程中发现,我用SDWebImage加载多个图片,类似微博动态那种,在加载的过程中。我发现当图片分辨率比较大的时候(不是图片大),加载几张图片就崩溃了。

网上说可以每次加载图片清空memcache,但是效果并不好。

[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];

这地方采用的方法是:

第一步:在 UIImage+MultiFormat这个类里面添加如下压缩方法

这个地方有些童鞋找不到就在上面先导入#import"UIImage+MultiFormat.h"然后command进去就能找到,直接拷贝下面的方法,然后在第二步的方法里调用就可以了

+(UIImage *)compressImageWith:(UIImage *)image

{

float imageWidth = image.size.width;

float imageHeight = image.size.height;

float width = 320;

float height = image.size.height/(image.size.width/width);

float widthScale = imageWidth /width;

float heightScale = imageHeight /height;

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(CGSizeMake(width, height));

if (widthScale > heightScale) {

[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];

}

else {

[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];

}

// 从当前context中创建一个改变大小后的图片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈

UIGraphicsEndImageContext();

return newImage;

}

第二步: 在下面这个方法里调用压缩方法

+ (UIImage *)sd_imageWithData:(NSData *)data {

UIImage *image;

NSString *imageContentType = [NSData sd_contentTypeForImageData:data];

if ([imageContentType isEqualToString:@"image/gif"]) {

image = [UIImage sd_animatedGIFWithData:data];

}

#ifdef SD_WEBP

else if ([imageContentType isEqualToString:@"image/webp"])

{

image = [UIImage sd_imageWithWebPData:data];

}

#endif

else {

image = [[UIImage alloc] initWithData:data];

if (data.length/1024 > 90) {

image = [self compressImageWith:image];

}

UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];

if (orientation != UIImageOrientationUp) {

image = [UIImage imageWithCGImage:image.CGImage

scale:image.scale

orientation:orientation];

}

}

return image;

}

第三步:

就是在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:

这里的方法也是先导入#import"SDWebImageDownloaderOperation.h",在点进去

UIImage *image = [UIImage sd_imageWithData:self.imageData];

NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];

image = [self scaledImageForKey:key image:image];

NSData *data = UIImageJPEGRepresentation(image, 1);

self.imageData = [NSMutableData dataWithData:data];

最后;再配合    

[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];(图片加载后使用)大功告成,亲测内存基本变化不大,自动释放也来得及。

然后确实很舒服了,也不会崩溃了,图片也比较清晰

你可能感兴趣的:(Restore the connection to "“XXX”的 iPod" and run "XXX" again, or if "XXX" is still running, you can attach to it by selecting Debug > Attach to Process > XXX.)