这段代码应该是将从picker视图中选择的图像的数据分成块并将其上传到网站,但每次我尝试上传一个特定的块时,它都会给我一个EXC_BAD_ACCESS.The下面是将图像数据分成块的代码
PrimaryImageController.h
@interface PrimaryImageViewController
{
__weak IBOutlet UIImageView *imgView;
}
@property (nonatomic,strong) NSMutableArray *chunkArray;
PrimaryImageController.m
@synthesize imgView,chunkArray;
- (void)viewDidLoad
{
chunkArray=[[NSMutableArray alloc]init];
}
-(void)updateImage
{
UIImage *img = imgView.image;
NSData *dataObj=UIImageJPEGRepresentation(img, 1.0);
NSUInteger length = [dataObj length];
NSUInteger chunkSize = 3072*10;
NSUInteger offset = 0;
int numberOfChunks=0;
do
{
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[dataObj bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
offset += thisChunkSize;
[chunkArray insertObject:chunk atIndex:numberOfChunks];
numberOfChunks++;
}
while (offset < length);
for (int i=0; i<[chunkArray count]; i++)
{
[uploadPrimary uploadImage:[chunkArray objectAtIndex:i] uuid:uniqueIdString numberOfChunks:[chunkArray count] currentChunk:i];
}
}发布于 2013-03-15 01:51:58
exc_bad_access表示硬崩溃,不多也不少。虽然过度释放对象通常会导致这种情况,但还有许多其他原因可能会发生这种崩溃。同样,硬崩溃也不是NSException意义上的异常;设置异常断点也无济于事。
如果你有一个崩溃,你应该有一个回溯。发布崩溃的回溯。
如果你启用了ARC,这看起来像是一个内部指针问题。您正在创建一系列对dataObj中包含的数据的引用,但再也不会引用dataObj了。
尝试在该for()循环之后添加[dataObj self];。
但是,由于您将块存储在一个实例变量数组中,因此dataObj的生存期应该与该数组的生存期相关联。也就是说,要么将数组移入updateImage方法,要么将iVar声明为强引用dataObj。
https://stackoverflow.com/questions/15416250
复制相似问题