我有一个问题,我似乎无法解决或找到任何SO相关的帖子。我有一个图像,我试图传递给一个函数,用CIFilter编辑并传回结果显示。然而,当图像被传回时,它似乎已被释放,我不知道为什么。这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
myImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIImage *image = [UIImage imageNamed:@"testim.png"];
UIImage *imout;
[self editimage: image fpin:imout];
myImageView.image = imout;
[self.view addSubview:myImageView];
}
-(void) editimage:(UIImage *) image fpin:(UIImage *) img_new{
CIImage *ciImage = [CIImage imageWithCGImage:[image CGImage]];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
keysAndValues: kCIInputImageKey, ciImage,
@"inputIntensity", @0.8, nil];
CIImage *outputImage = [filter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimage = [context createCGImage:outputImage fromRect:[outputImage extent]];
img_new = [UIImage imageWithCGImage:cgimage];
CGImageRelease(cgimage);
}
有人可以对此有所了解吗?
答案 0 :(得分:0)
您的代码存在问题,实际上它根本不会改变您的输出图像。您的editimage
方法会将图片分配给imout
的本地副本。
您有两种选择:
选项1
返回修改后的图片:
-(UIImage*) editimage:(UIImage *) image {
...
UIImage *newImage = [UIImage imageWithCGImage:cgimage];
CGImageRelease(cgimage);
return newImage
}
在你的viewDidLoad中:
UIImage *imout = [self editimage:image];
选项2
如果您真的想坚持使用原始图案,可以将参考传递给图像:
-(void) editimage:(UIImage *) image fpin:(UIImage **) img_new {
...
*img_new = [UIImage imageWithCGImage:cgimage];
CGImageRelease(cgimage);
}
在你的viewDidLoad中:
[self editimage: image fpin:&imout];