[英]Issues passing UIImage after using CIFilter
我有一個問題,我似乎無法解決或找到任何與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);
}
誰能對此有所啟發?
您的代碼的問題在於,實際上它根本不會改變您的輸出圖像。 你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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.