经过多天将我的头撞在墙上并度过了一个不眠之夜,我希望能在这里找到一些帮助.我在这里经历了各种各样的帖子,但没有一个答案似乎为我提供了解决方案.
简而言之,我的问题是我的应用程序在UIWebView大量使用(> 10分钟)后崩溃(例如,逐个打开更大的新闻纸张网站 - 一个接一个).
为了提供更多细节:我正在编写一个iPhone应用程序,并从MainViewController我在navigationController上推送一个browserViewController.browserViewController加载一个包含UWebView的笔尖(我不以编程方式创建WebView).UIWebView使用Interface Builder连接.
当回到Main然后再次访问browserViewController时,我只重新创建了browserViewController(如果它是nil).(我想保留在UIWebView中加载的内容 - 只有在有内存警告时才会卸载此视图并释放所有使用的内存).
在MainViewController和browserViewController中我都在响应内存警告,但这似乎没有提供足够的缓解.
看看仪器,我注意到例如CFData(商店)不断增加.即使我模拟了内存警告(请参阅下面的代码)并在browserViewController上调用viewDidUnload,CFData仍然会被分配并且不会被释放.
所以我最大的问题是:
如何释放"浏览"创建的内存?
这有两种情况:
- 如何确保viewDidUnload正确释放分配了我的CFData(存储)的内存?
- 当用户继续在browserViewController中加载页面时如何释放内存?
.
谁管理CFData?
请参阅下面的简化示例代码:
// MainViewController.h
#import "myAppDelegate.h"
#import "BrowserViewController.h"
@interface MainViewController : UIViewController {
BrowserViewController *browViewController;
}
- (void) switchToBrowserViewController;
@end
Run Code Online (Sandbox Code Playgroud)
// MainViewController.m
#import "MainViewController.h"
@implementation MainViewController
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
[browViewController release];
browViewController = nil;
}
- (void)viewDidUnload {
// Release any retained subviews of the main …Run Code Online (Sandbox Code Playgroud) 我一直在寻找解决方案来解决下面的问题,但找不到.我做了过滤方法来修改HSV值UIImage.这种方法工作正常,但是当我使用这种方法时,使用的内存会长大,无法释放.在Instruments的"Live Bytes"框中,我发现CFData(store)的值为每次过滤增加了几MB.我怎么才能释放他们?我发现了类似的问题,但对这个问题不太好.谢谢.
//Objective-C
- (UIImage*)addFilter:(UIImage*)img{
CGImageRef imgRef = img.CGImage;
//getting size
size_t width = CGImageGetWidth(imgRef);
size_t height = CGImageGetHeight(imgRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(imgRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imgRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imgRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imgRef);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgRef);
bool shouldInterpolate = CGImageGetShouldInterpolate(imgRef);
CGColorRenderingIntent renderingIntent = CGImageGetRenderingIntent(imgRef);
CGDataProviderRef dataProvider = CGImageGetDataProvider(imgRef);
CFMutableDataRef *data = CGDataProviderCopyData(dataProvider);
UInt8 *buf = (UInt8 *)CFDataGetMutableBytePtr(data);
double h,s,v;
double c;
double cmax,cmin;
for (int j = 0; j < height; …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照此答案访问内在矩阵。
通过运行下面的命令,我获得了一个 48 字节的AnyObject,并进一步将其转换为CFData。
let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil)
Run Code Online (Sandbox Code Playgroud)
但是,我检查了sampleBuffer的输出CMSampleBuffer.h:
/*! @constant kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix
@abstract Indicates the 3x3 camera intrinsic matrix applied to the current sample buffer.
@discussion Camera intrinsic matrix is a CFData containing a matrix_float3x3, which is column-major.
....
*/
CM_EXPORT const CFStringRef kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix // CFData (matrix_float3x3) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_11_0);
Run Code Online (Sandbox Code Playgroud)
matrix_float3x3我应该如何访问from中的值CFData?
我有一个应用程序,可以将各种过滤器应用于图像.它在iOS 5上运行良好但在6上崩溃.下面是它崩溃的地方示例:
CGImageRef inImage = self.CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef);
for (int i=0; i<length; i+=4)
{
if(filter == filterCurve){
int r = i;
int g = i+1;
int b = i+2;
int red = m_PixelBuf[r];
int green = m_PixelBuf[g];
int blue = m_PixelBuf[b];
m_PixelBuf[r] = SAFECOLOR(red); // <==== EXC_BAD_ACCESS (code = 2)
m_PixelBuf[g] = SAFECOLOR(green);
m_PixelBuf[b] = SAFECOLOR(blue);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将值重新分配给时,请注意错误的访问点m_PixelBuf.任何人都知道为什么会这样?iOS 6中的内容会导致什么?
我在代码中有一行被弃用了,在XCode中有什么建议要替换它,但我无法理解差异,这些是我的三行有效:
let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData.dataWithContentsOfMappedFile(path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef
Run Code Online (Sandbox Code Playgroud)
现在根据警告和建议,我将我的代码更改为:
let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData(contentsOfFile: path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
Run Code Online (Sandbox Code Playgroud) 我有一些关于从iPhone应用程序中提供的PDF文档中专门提取图像(仅图像)的疑问.
我已经浏览了苹果的文档 - 但我没有找到它.
我已经完成了从PDF文档获取图像的努力.
-(IBAction)btnTappedImages:(id)sender{
// MyGetPDFDocumentRef is custom c method
// & filePath is path to pdf document.
CGPDFDocumentRef document = MyGetPDFDocumentRef ([filePath UTF8String]);
int pgcnt = CGPDFDocumentGetNumberOfPages( document );
for( int i1 = 0; i1 < pgcnt; ++i1 ) {
// 1. Open Document page
CGPDFPageRef pg = CGPDFDocumentGetPage (document, i1+1);
if( !pg ) {
NSLog(@"Couldn't open page.");
}
// 2. get page dictionary
CGPDFDictionaryRef dict = CGPDFPageGetDictionary( pg );
if( !dict ) {
NSLog(@"Couldn't open page …Run Code Online (Sandbox Code Playgroud) 当我返回一个CFDataRef由
(CFDataRef)MyFunction{
.....
CFDataRef data = CFDataCreate(NULL, buf, bufLen);
free(buf);
return data;
}
Run Code Online (Sandbox Code Playgroud)
有内存泄漏,如何进行CFDataRef自动释放?方法[data autorelease]不会退出.
我想用 Swift 创建一些 PDF 页面。
这是我创建 PDF 页面的代码
// Initialize context and other stuff
CGPDFContextBeginPage(pdfContext, aDictionary);
CGContextDrawPDFPage(pdfContext, pdfPageRef)
CGPDFContextEndPage(pdfContext)
Run Code Online (Sandbox Code Playgroud)
我对“aDictionary”变量有疑问。我想在 kCGPDFContextMediaBox 键下添加媒体框(CGRect 类型变量)值。该文档告诉我以下内容:
The media box for the document or for a given page. This key is optional. If present, the value of this key must be a CFData object that contains a CGRect (stored by value, not by reference).
在 Objective-C 中,从 CGRect 创建 CFData 相当容易,但在 Swift 中我不知道如何创建它。这是我的 Objective-C 代码:
CGRect mediaBox = CGRectZero;
CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&mediaBox, sizeof(mediaBox)); …Run Code Online (Sandbox Code Playgroud) 此代码类型检查并编译,然后崩溃。如何保存CGImage到,Data以便以后可以再次阅读。
let cgi: CGImage? = ...
var mData = Data()
let imageDest = CGImageDestinationCreateWithData(mData as! CFMutableData,
kUTTypePNG, 1, nil)!
CGImageDestinationAddImage(imageDest, cgi!, nil)
CGImageDestinationFinalize(imageDest)
Run Code Online (Sandbox Code Playgroud)
最后一行崩溃。控制台错误是:
2018-01-17 19:25:43.656664-0500 HelloPencil[2799:3101092] -[_NSZeroData
appendBytes:length:]: unrecognized selector sent to instance 0x1c80029c0
2018-01-17 19:25:43.658420-0500 HelloPencil[2799:3101092] *** Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[_NSZeroData appendBytes:length:]: unrecognized selector
sent to instance 0x1c80029c0'
Run Code Online (Sandbox Code Playgroud)
Xcode建议使用从Data到的转换CFMutableData,但这也许是错误的。
cfdata ×9
ios ×5
swift ×3
iphone ×2
autorelease ×1
casting ×1
cfdictionary ×1
cgimage ×1
ios11 ×1
ios6 ×1
memory ×1
memory-leaks ×1
nsdata ×1
objective-c ×1
pdf ×1
swift2 ×1
swift4 ×1
uiimage ×1
uiwebview ×1
xcode ×1