gpt4 book ai didi

ios - 我的 iphone 应用程序占用了太多内存

转载 作者:太空宇宙 更新时间:2023-11-03 21:54:02 25 4
gpt4 key购买 nike

我使用 UICollectionView 制作了画廊应用程序,但我在分配方面表现不佳,如下所示。

enter image description here

我找不到哪里不好。我应该在哪里明确释放对象?请告诉我。

以下代码对此表示怀疑。

在 Collection View 中,

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
...
dispatch_async(all_queue, ^{
ALAssetRepresentation *representation = [asset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[representation fullResolutionImage]
scale:[representation scale]
orientation:(UIImageOrientation)[representation orientation]];
NSString *filename = [representation filename];
NSLog(@"%@", filename);
NSLog(@"Loaded Image row : %d", indexPath.row);

vector<cv::Rect> faces = [ImageUtils findFeature:image minsize:MIN_FACE_SIZE
withCascade:face_cascade];

Mat imageMat = [ImageUtils cvMatFromUIImage:image];
for(unsigned int i = 0; i < es.size(); ++i) {
rectangle(imageMat, cv::Point(es[i].x, es[i].y),
cv::Point(es[i].x + es[i].width, es[i].y + es[i].height),
cv::Scalar(0,255,255),5);
}
dispatch_async(dispatch_get_main_queue(), ^{
[faceImage setImage:[ImageUtils UIImageFromCVMat:imageMat]];
[cell setNeedsDisplay];
});
});
return cell;
}

调用方法

+ (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;

cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)

CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags

CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

return cvMat;
}

另一种方法

+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;

if (cvMat.elemSize() == 1) {
colorSpace = CGColorSpaceCreateDeviceGray();
} else {
colorSpace = CGColorSpaceCreateDeviceRGB();
}

CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
cvMat.rows, //height
8, //bits per component
8 * cvMat.elemSize(), //bits per pixel
cvMat.step[0], //bytesPerRow
colorSpace, //colorspace
kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
provider, //CGDataProviderRef
NULL, //decode
false, //should interpolate
kCGRenderingIntentDefault //intent
);


// Getting UIImage from CGImage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);

return finalImage;
}

另一种方法

+(cv::vector<cv::Rect>)findFeature:(UIImage *)image minsize:(cv::Size)minSize withCascade:(CascadeClassifier)cascade
{
vector<cv::Rect> faces;
Mat frame_gray;
Mat imageMat = [ImageUtils cvMatFromUIImage:image];

cvtColor(imageMat, frame_gray, CV_BGRA2GRAY);
equalizeHist(frame_gray, frame_gray);

cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, minSize);
frame_gray.release();
imageMat.release();

return faces;
}

最佳答案

这是因为你的图片分辨率太高了。你必须找到一种方法来减小它的大小。

关于ios - 我的 iphone 应用程序占用了太多内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21619233/

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