Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 什么';ARC中的这项交易有什么问题?_Ios_Cocoa Touch_Memory Management_Grand Central Dispatch_Nsthread - Fatal编程技术网

Ios 什么';ARC中的这项交易有什么问题?

Ios 什么';ARC中的这项交易有什么问题?,ios,cocoa-touch,memory-management,grand-central-dispatch,nsthread,Ios,Cocoa Touch,Memory Management,Grand Central Dispatch,Nsthread,我正在开发一个应用程序,它可以进行图像处理并显示结果图像。Im使用UIScrollView让用户滚动所有图像,因为图像不是标准的jpg或png,加载需要时间。我使用GCD异步加载,当完成分配到主队列以显示时。片段如下所示: - (void)loadImage:(NSString *)name { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage

我正在开发一个应用程序,它可以进行图像处理并显示结果图像。Im使用
UIScrollView
让用户滚动所有图像,因为图像不是标准的jpg或png,加载需要时间。我使用GCD异步加载,当完成分配到主队列以显示时。片段如下所示:

- (void)loadImage:(NSString *)name
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [Reader loadImage:name];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self displayImage:image];
        });
    });
}
阅读器的
loadImage
方法如下:

+ (UIImage *)loadImage:(NSString *)name
{
   UInt8 *data = NULL;
   NSString *mfjPath = [TMP stringByAppendingPathComponent:name];
   NSData *mfjData = [NSData dataWithContentsOfFile:mfjPath];
   if(mfjData){
        data = malloc(sizeof(UInt8)*mfjData.length);
        [mfjData getBytes:data];
   }
   if(data){
        ResultHolder *result = [sDecoder decodeData:data withOffset:0];// static id<IDecoder> sDecoder; in Reader.m before @implementation Reader.
        return [result bitmap];
   }
    retun nil;
}
ResultHolder
是一个加载简单图像并组合复杂图像的类。详情如下:

- (void)loadImage:(NSString *)name
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [Reader loadImage:name];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [self displayImage:image];
        });
    });
}
ResultHolder.h

typedef struct color24{
    UInt8 R;
    UInt8 G;
    UInt8 B;
} Color24;

@interface ResultHolder : NSObject
{
    unsigned long mWidth;
    unsigned long mHeight;
    UInt8 *mData;
    CGImageRef mBitmap;

    BOOL isMonoColor;
    Color24 mMonoColor;
}

+ (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image;
+ (ResultHolder *)resultHolderWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height;
+ (ResultHolder *)resultHolderWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height;

- (ResultHolder *)initWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long) height;
- (ResultHolder *)initWithCGImage:(CGImageRef)image;
- (ResultHolder *)initWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height;

- (BOOL)isSuccess;
- (UIImage *)bitmap;
- (void)combineFixResultHolder:(ResultHolder *)child Rect:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height;
- (void)combineResultHolder:(ResultHolder *)child Bounds:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height;
@end
ResultHolder.m

@implementation ResultHolder

@synthesize width = mWidth;
@synthesize height = mHeight;
@synthesize isMonoColor;
@synthesize monoColor = mMonoColor;

- (ResultHolder *)initWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height
{

    if (self = [super init]) {        
        mWidth = width;
        mHeight = height;
        mData = malloc(mWidth*mHeight*sizeof(Color24));
        memcpy(mData, data, mWidth*mHeight*sizeof(Color24));

        mBitmap = NULL;
    }

    return self;
}

- (ResultHolder *)initWithCGImage:(CGImageRef)image
{
    if (self = [super init]) {
        mBitmap = CGImageRetain(image);
        mWidth = CGImageGetWidth(image);
        mHeight = CGImageGetHeight(image);
    }
    return self;
}

- (ResultHolder *)initWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height
{
    if (self = [super init]) {
        mMonoColor = monoColor;

        isMonoColor = YES;
        mWidth = width;
        mHeight = height;
        mBitmap = NULL;
        mData = NULL;
    }

    return self;
}

+ (ResultHolder *)resultHolderWithCGImage:(CGImageRef)image
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithCGImage:image];
    return resultHolder;
}

+ (ResultHolder *)resultHolderWithData:(UInt8 *)data Width:(unsigned long)width andHeight:(unsigned long)height
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithData:data Width:width andHeight:height];
    return resultHolder;
}

+ (ResultHolder *)resultHolderWithMonoColor:(Color24)monoColor withWidth:(unsigned long)width andHeight:(unsigned long)height
{
    ResultHolder *resultHolder = [[ResultHolder alloc] initWithMonoColor:monoColor withWidth:width andHeight:height];
    return resultHolder;
}

- (BOOL)isSuccess
{
    if ([ReaderConfigures CodecDebug])
        NSLog(@"ResultHolder isSuccess");
    return (mData != NULL || isMonoColor || mBitmap != nil);
}

- (void)fillMonoColor
{

    if (isMonoColor) {
        if (mData) {
            free(mData);
        }
        mData = (UInt8 *)malloc(mWidth*mHeight*sizeof(Color24));

        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                memcpy(mData+(i*mWidth+j)*3, &mMonoColor, sizeof(Color24));
            }
        }
        isMonoColor = NO;
    }
}


- (void)extractBitmap  
{
    if (mBitmap) {

        CGDataProviderRef dataProvider = CGImageGetDataProvider(mBitmap);
        CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
        UInt8 * dataSource = (UInt8 *)CFDataGetBytePtr(bitmapData);

        size_t width = CGImageGetWidth(mBitmap);
        size_t height = CGImageGetHeight(mBitmap);
        if(mData)
            free(mData);
        mData = malloc(width*height*3);

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                memcpy(mData+(i*width+j)*3, dataSource+(i*width+j)*4, sizeof(Color24));
            }
        }

        CFRelease(bitmapData);
        CGImageRelease(mBitmap);
        mBitmap = NULL;
    }
}


- (UInt8 *)getRawData
{

    if (mBitmap) {
        [self extractBitmap];
    }
    if (isMonoColor) {
        [self fillMonoColor];
    }

    return mData;
}

- (UIImage *)bitmap
{
    if (mBitmap) {
        UIImage *image = [[UIImage alloc] initWithCGImage:mBitmap];
        CGImageRelease(mBitmap);
        mBitmap = NULL;
        return image; 
    }
    if (isMonoColor) {
        [self fillMonoColor];
    }
    if (mData) {
        CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, mData, mWidth*mHeight*3, NULL);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGImageRef bitmap = CGImageCreate(mWidth, mHeight, 8, 24, mWidth*3, colorSpace, kCGBitmapByteOrderDefault, dataProvider, NULL, YES, kCGRenderingIntentDefault);
        CGColorSpaceRelease(colorSpace);
        CGDataProviderRelease(dataProvider);
        UIImage *image = [[UIImage alloc] initWithCGImage:bitmap];
        CGImageRelease(bitmap);

        return image;
    }

    return nil;
}

- (void)combineResultHolder:(ResultHolder *) child Bounds:(CGRect) bounds Width:(unsigned long)width andHeight:(unsigned long)height
{
    CGRect rect = CGRectMake(MAX(0, bounds.origin.x), MAX(0, bounds.origin.y),MIN(width - 1, bounds.origin.x + bounds.size.width), MIN(height - 1, bounds.origin.y + bounds.size.height));

    int w = MIN(rect.size.width + 1, child.width);
    int h = MIN(rect.size.height + 1, child.height);

    int dstPos = (height - 1 - (rect.origin.y + h - 1))*width;

    UInt8 *dataParent = [self getRawData];

    if (child.isMonoColor) {
        Color24 childMonoColor = child.monoColor;
        for (int i = 0; i < h; i++) {
            memcpy(dataParent+(dstPos+(int)rect.origin.x)*3, &childMonoColor, w*3);
            dstPos += width;
        }

    } else {
        UInt8 *dataChild = [child getRawData];
        if (dataChild != nil) {
            int srcPos = 0;
            for (int i = 0; i < h; i++) {
                memcpy(dataParent+dstPos*3+((int)rect.origin.x)*3, dataChild+srcPos*3, w*3);
                srcPos += child.width;
                dstPos += width;
            }
        }

    }

}

- (void)combineFixResultHolder:(ResultHolder *)child Rect:(CGRect)bounds Width:(unsigned long)width andHeight:(unsigned long)height
{
    CGRect rect = CGRectMake(bounds.origin.x, height-1-bounds.origin.y-bounds.size.height, bounds.origin.x+bounds.size.width, height-1-bounds.origin.y);

    [self combineResultHolder:child Bounds:rect Width:width andHeight:height];
}

- (void)dealloc
{
    if (mData) {
        free(mData);
        mData = NULL;
    }
    if (mBitmap) {
        CGImageRelease(mBitmap);
        mBitmap = NULL;
    }
}

@end
@实现结果文件夹
@合成宽度=mWidth;
@综合高度=mHweight;
@合成isMonoColor;
@合成单色=氨色;
-(ResultHolder*)initWithData:(UInt8*)数据宽度:(无符号长)宽度和高度:(无符号长)高度
{
if(self=[super init]){
mWidth=宽度;
mHeight=高度;
mData=malloc(mWidth*mHeight*sizeof(Color24));
memcpy(mData,data,mWidth*mHeight*sizeof(Color24));
mBitmap=NULL;
}
回归自我;
}
-(ResultHolder*)initWithCGImage:(CGImageRef)图像
{
if(self=[super init]){
mBitmap=cgimagerain(图像);
mWidth=CGImageGetWidth(图像);
mHeight=CGImageGetHeight(图像);
}
回归自我;
}
-(ResultHolder*)initWithMonoColor:(Color24)width:(无符号长)width和height:(无符号长)height单色
{
if(self=[super init]){
单色=单色;
isMonoColor=是;
mWidth=宽度;
mHeight=高度;
mBitmap=NULL;
mData=NULL;
}
回归自我;
}
+(ResultHolder*)带有CGImage的ResultHolder:(CGImageRef)图像
{
ResultHolder*ResultHolder=[[ResultHolder alloc]initWithCGImage:image];
返回结果文件夹;
}
+(ResultHolder*)ResultHolder with data:(UInt8*)数据宽度:(无符号长)宽度和高度:(无符号长)高度
{
ResultHolder*ResultHolder=[[ResultHolder alloc]initWithData:data-Width:Width和height:height];
返回结果文件夹;
}
+(ResultHolder*)ResultHolder With单色:(Color24)width单色:(无符号长)width and height:(无符号长)height
{
ResultHolder*ResultHolder=[[ResultHolder alloc]initWithMonoColor:monoColor withWidth:width和height:height];
返回结果文件夹;
}
-(BOOL)isSuccess
{
if([ReaderConfigures CodeDebug])
NSLog(@“ResultHolder isSuccess”);
返回值(mData!=NULL | | isMonoColor | | mBitmap!=nil);
}
-(空白)单色
{
if(isMonoColor){
如果(mData){
免费(mData);
}
mData=(UInt8*)malloc(mWidth*mHeight*sizeof(Color24));
对于(int i=0;iif (index > [mPageNames count]) {
2012-09-26 08:55:12.378 ReaderDemo[787:11303] MFJAtIndex index out of bounds,index:15,bounds:15
2012-09-26 08:55:12.379 ReaderDemo[787:11303] *** Assertion failure in -[ImageScrollView showLoadingForMFJ:], /Volumes/Data/Users/dhoerl/Downloads/ReaderDemo/ReaderDemo/ImageScrollView.m:247
CGDataProviderReleaseDataCallback
A callback function that releases data you supply to the function CGDataProviderCreateWithData.

typedef void (*CGDataProviderReleaseDataCallback) (
   void *info,
   const void *data,
   size_t size
);