gpt4 book ai didi

ios - 使用__block变量阻止泄漏

转载 作者:行者123 更新时间:2023-12-01 17:51:34 24 4
gpt4 key购买 nike

我发现在requestContentEditingInputWithOptions:方法中/上发生了很大的内存泄漏。如果我理解正确,它会发生在img变量中。如果将其设置为__block __weak,则在将其分配给(img = [UIImage...])后,该图像已经为nil。我在某个地方傻吗?还是我将如何避免这种内存泄漏?

- (UIImage*) getRightlySizedImgFromAsset:(PHAsset*)asset {

__block UIImage *img;


PHContentEditingInputRequestOptions *coptions = [PHContentEditingInputRequestOptions new];
coptions.canHandleAdjustmentData = ^BOOL(PHAdjustmentData *adjustmentData) { return NO; };

//semaphore used so the block runs synchronously and I can return img from this method at the end
dispatch_semaphore_t sem = dispatch_semaphore_create(0);

[asset requestContentEditingInputWithOptions:coptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL* url = [contentEditingInput fullSizeImageURL];
int orientation = [contentEditingInput fullSizeImageOrientation];

CIImage* inputImage = [CIImage imageWithContentsOfURL:url options:nil];
inputImage = [inputImage imageByApplyingOrientation:orientation];
CIContext *context = [CIContext contextWithOptions:nil];

img = [UIImage imageWithCGImage:[context createCGImage:inputImage fromRect:inputImage.extent]];

dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);


if (needToDoSomethingWithImg){
[self doSomethingWithImage:img];
}


return img;
}

最佳答案

通过静态分析器运行此代码(shift + command + B或从“产品”菜单中选择“分析”),它将指出createCGImage正在创建您永远不会释放的CGImageRef

您可能想要执行以下操作:

CGImageRef imageRef = [context createCGImage:inputImage fromRect:inputImage.extent];
img = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);

顺便说一句,您不应该同步执行此操作。您应该执行以下操作:
- (void) getRightlySizedImgFromAsset:(PHAsset*)asset completionHandler:(void (^)(UIImage *))completionHandler {

PHContentEditingInputRequestOptions *coptions = [PHContentEditingInputRequestOptions new];
coptions.canHandleAdjustmentData = ^BOOL(PHAdjustmentData *adjustmentData) { return NO; };

[asset requestContentEditingInputWithOptions:coptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL* url = [contentEditingInput fullSizeImageURL];
int orientation = [contentEditingInput fullSizeImageOrientation];

CIImage* inputImage = [CIImage imageWithContentsOfURL:url options:nil];
inputImage = [inputImage imageByApplyingOrientation:orientation];
CIContext *context = [CIContext contextWithOptions:nil];

CGImageRef imageRef = [context createCGImage:inputImage fromRect:inputImage.extent];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CFRelease(imageRef);

// if this stuff needs to happen on main thread, then dispatch it to the main thread

if (needtodosomethingwithit)
[self doSomethingWithImage:image];

if (completionHandler) {
completionHandler(image);
}
}];
}

关于ios - 使用__block变量阻止泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com