【发布时间】:2016-04-28 17:22:29
【问题描述】:
我有以下代码:
- (void)applySepiaFilter {
// Set previous image
NSData *buffer = [NSKeyedArchiver archivedDataWithRootObject: self.mainImage.image];
[_images push:[NSKeyedUnarchiver unarchiveObjectWithData: buffer]];
[_images push:[NSKeyedUnarchiver unarchiveObjectWithData: buffer]];
UIImage* u = [_images pop];
CIImage *image = [[CIImage alloc] initWithCGImage:u.CGImage];//[CIImage imageWithContentsOfURL:fileNameAndPath];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
keysAndValues: kCIInputImageKey, image,
@"inputIntensity", @0.8, nil];
CIImage *outputImage = [filter outputImage];
//UIImage *newImage =
self.mainImage.image = [UIImage imageWithCIImage:outputImage];
}
其中 _images 是一个堆栈:
#import "Stack.h"
@implementation Stack{
NSMutableArray *stack;
}
-(id)init{
self = [super init];
if(self!=nil){
stack = [[NSMutableArray alloc] init];
}
return self;
}
-(void)push:(id)obj{
[stack addObject:obj];
}
-(id)pop{
id lastObj = [stack lastObject];
if (stack.count > 1) {
[stack removeLastObject];
}
return lastObj;
}
-(NSUInteger)size{
return stack.count;
}
@end
self.mainImage 是一个 UIImageView。
第一次运行该方法时,我得到了所需的棕褐色过滤器。我相信它没有正确保存 self.mainImage.image,因为如果我连续两次运行该方法,我会得到 self.mainImage.image 的空白图像,如果我第三次运行它,我会收到 SIGABRT 错误。 我添加了一个断点并开始遍历代码。我注意到在 self.mainImage.image 方法的第一遍结束时是一个 CIImage *。这让我很困惑,因为它应该是 UIImage *(见图)。Screenshot during debug
让我担心的另一件事是,我注意到缓冲区大小明显小于第二遍时的大小。这让我相信图像没有被保存。同样在第二遍期间,调试器说 *imge 为空。 (见截图)Debugging screenshot during second pass
【问题讨论】:
-
将图像转换回
CGImage并从中创建UIImage
标签: ios objective-c memory uiimage ciimage