通过 SDWebImage 来做图片下载和缓存,SDWebImage 在下载完成后作的图片缓存是异步的, 也就是在第一次下载图片的时候, 本地是没有缓存的, 而且处理的图片是在内存里面的, 然后处理图片的时候出现了坏内存访问. 而在第二次处理的时候之前一次已经将图片缓存到本地了, 所以就没有问题了. 虽然我也不知道为什么这个处理方法对没有缓存到本地的图片处理会出现坏内存访问, 不过最终把问题定位到这个地方了.
解决方案:在SDWebImage下载完图片后, 先检查本地有没有存在该图片, 如果没有, 则先把下载下来的图片同步缓存到本地, 然后再从本地取得图片进行处理.问题解决!
SDWebImageManager sharedManager] downloadImageWithURL:imageUrl options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
NSString* key = [[SDWebImageManager sharedManager] cacheKeyForURL:imageURL];
BOOL result = [[SDImageCache sharedImageCache] diskImageExistsWithKey:key];
NSString* imagePath = [[SDImageCache sharedImageCache] defaultCachePathForKey:key];
NSData* newData = [NSData dataWithContentsOfFile:imagePath];
if (!result || !newData) {
BOOL imageIsPng = ImageDataHasPNGPreffixWithData(nil);
NSData* imageData = nil;
if (imageIsPng) {
imageData = UIImagePNGRepresentation(image);
}
else {
imageData = UIImageJPEGRepresentation(image, (CGFloat)1.0);
}
NSFileManager* _fileManager = [NSFileManager defaultManager];
if (imageData) {
[_fileManager removeItemAtPath:imagePath error:nil];
[_fileManager createFileAtPath:imagePath contents:imageData attributes:nil];
}
}
newData = [NSData dataWithContentsOfFile:imagePath];
UIImage* grayImage = nil;
if (type == 0) {
grayImage = [UIImage imageWithData:newData];
}else{
UIImage* newImage = [UIImage imageWithData:newData];
grayImage = [newImage grayscaleWithType:type];//对于进行图片处理的方法
}
self.imageView.image = grayImage;
}
ios通过URL地址,从网络上获取图片
(UIImage *) getImageFromURL:(NSString *)fileURL {
NSLog(@"执行图片下载函数");
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
获取网络图片,然后进行模糊处理
#import "ViewController.h"
#import "FXBlurView.h"
#import
#import "UIImageView+WebCache.h"
@interface ViewController ()
@property (nonatomic,retain)UIImageView *imageView;
@property (nonatomic,retain)FXBlurView *blur;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[_imageView sd_setImageWithURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg"] placeholderImage:[UIImage imageNamed:@"xingkong"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[self.view addSubview:_imageView];
_blur = [[FXBlurView alloc]initWithFrame:CGRectMake(50, 50,200,200)];
_blur.blurRadius = 20;
_blur.dynamic = NO;
_blur.tintColor = [UIColor clearColor];
[self.view addSubview:_blur];
}];
}
-(UIImage * ) getImageFromURL:(NSString *)fileURL
{
UIImage *image;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
image = [UIImage imageWithData:data];
return image;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
压缩图片
CGFloat scaleFloat = 0.6;
+ (UIImage *) scaleImage: (UIImage *)image scaleFactor:(float)scaleFloat
{
CGSize size = CGSizeMake(image.size.width * scaleBy, image.size.height * scaleBy);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, scaleBy, scaleBy);
CGContextConcatCTM(context, transform);
// Draw the image into the transformed context and return the image
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
然后�*****mgData = UIImageJPEGRepresentation(image, scaleFloat