Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 将UIImage转换为二进制,接收EXC\u BAD\u访问_Ios_Uiimage - Fatal编程技术网

Ios 将UIImage转换为二进制,接收EXC\u BAD\u访问

Ios 将UIImage转换为二进制,接收EXC\u BAD\u访问,ios,uiimage,Ios,Uiimage,我正在使用source from向用户展示一个视图,用户可以在其中签名。然后我将签名的图像发送到Heroku上运行的服务器。这在模拟器中运行良好。当我在实际设备(iPad)上测试时,应用程序会在以下行崩溃:UIImagePNGRepresentation(signature) 处理签名提交的控制器方法: (signatureView是从前面提到的github repo下载的NISignatureView的一个实例) NISignatureView的getSignatureImage方法: - (

我正在使用source from向用户展示一个视图,用户可以在其中签名。然后我将签名的图像发送到Heroku上运行的服务器。这在模拟器中运行良好。当我在实际设备(iPad)上测试时,应用程序会在以下行崩溃:
UIImagePNGRepresentation(signature)

处理签名提交的控制器方法: (signatureView是从前面提到的github repo下载的NISignatureView的一个实例)

NISignatureView的getSignatureImage方法:

- (UIImage *)getSignatureImage {
if (!self.drawnSignature)
    return nil;

CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
NSInteger myDataLength = width * height * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width * 4; x++) {
        buffer2[(((int) height - 1) - y) * (int) width * 4 + x] = buffer[y * 4 * (int) width + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [[UIImage alloc] initWithCGImage:imageRef];

// release memory
CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
free(buffer);
free(buffer2);

return myImage;
-(UIImage*)getSignatureImage{
如果(!自身图纸签名)
返回零;
CGFloat width=self.frame.size.width;
CGFloat高度=self.frame.size.height;
NSInteger myDataLength=宽度*高度*4;
//分配数组并将像素读入其中。
GLubyte*buffer=(GLubyte*)malloc(myDataLength);
glReadPixels(0,0,宽度,高度,GL_RGBA,GL_无符号字节,缓冲区);
//gl呈现“倒置”,以便从上到下交换到新阵列中。
//肯定有更好的办法,但这是可行的。
GLubyte*buffer2=(GLubyte*)malloc(myDataLength);
对于(int y=0;y
}

从控制器调用的帮助程序:

- (void)sendSignature:(UIImage *)signature total:(NSNumber *)total orderId:(NSNumber *)orderId authToken:(NSString *)authToken successBlock:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))successBlock failureBlock:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failureBlock view:(UIView *)view {
    NSData *imageData = UIImagePNGRepresentation(signature);
    MBProgressHUD *submit = [MBProgressHUD showHUDAddedTo:view animated:YES];
    submit.labelText = @"Saving";
    [submit show:NO];
    NSString *url = [NSString stringWithFormat:@"%@?%@=%@", [NSString stringWithFormat:kDBCAPTURESIG([orderId intValue])], kAuthToken, authToken];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
    [client setParameterEncoding:AFJSONParameterEncoding];
    NSDictionary *parameters = @{@"total" : total};
    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"" parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"signature" fileName:@"signature" mimeType:@"image/png"];
    }];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *req, NSHTTPURLResponse *response, id json) {
                                                                                            [submit hide:NO];
                                                                                            if (successBlock) successBlock(req, response, json);
                                                                                        } failure:^(NSURLRequest *req, NSHTTPURLResponse *response, NSError *error, id json) {
                [submit hide:NO];
                if (failureBlock) failureBlock(req, response, error, json);
                NSInteger statusCode = [[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode];
                NSString *alertMessage = [NSString stringWithFormat:@"There was an error processing this request. Status Code: %d", statusCode];
                [[[UIAlertView alloc] initWithTitle:@"Error!" message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            }];
    [operation start];
}
-(void)sendSignature:(UIImage*)签名总数:(NSNumber*)orderId:(NSNumber*)orderId authToken:(NSString*)authToken successBlock:(void(^)(NSURLRequest*请求,nshtpurlresponse*响应,id JSON))successBlock failureBlock:(void(^)(NSURLRequest*请求,nshtpurlresponse*响应,NSError*错误,id JSON))failureBlock视图:(UIView*)视图{
NSData*imageData=UIIMAGEPNGRE表示(签名);
MBProgressHUD*submit=[MBProgressHUD showhuddedto:view animated:YES];
submit.labelText=@“正在保存”;
[提交节目:否];
NSString*url=[NSString stringWithFormat:@“%@?%@=%@”,[NSString stringWithFormat:kDBCAPTURESIG([orderId intValue]),kAuthToken,authToken];
AFHTTPClient*client=[[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:url]];
[客户端设置参数编码:afjson参数编码];
NSDictionary*参数=@{@“总计”:总计};
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@”“参数:参数constructingBodyWithBlock:^(id

更新 应用程序并不是每次都崩溃。崩溃发生在同一行,但只是偶尔发生。我已经更新了描述,以包括NISignatureView的getSignatureImage方法。一些对象在该方法结束时被释放。我想知道这是否与错误的访问错误有关。我很惭愧地说,我还不了解发布并保留。 更新2 我在NISignatureView的NISignatureView属性的
getSignatureImage
中释放或释放对象。将释放/释放逻辑移动到一个单独的方法。在我将图像发送到服务器后调用此方法。这似乎可行。没有遇到崩溃。但我不知道我所做的是正确的方法来解决此问题。签名视图是否需要释放这些对象?在卸载视图时它们不会自动释放吗

- (void)sendSignature:(UIImage *)signature total:(NSNumber *)total orderId:(NSNumber *)orderId authToken:(NSString *)authToken successBlock:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))successBlock failureBlock:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failureBlock view:(UIView *)view {
    NSData *imageData = UIImagePNGRepresentation(signature);
    MBProgressHUD *submit = [MBProgressHUD showHUDAddedTo:view animated:YES];
    submit.labelText = @"Saving";
    [submit show:NO];
    NSString *url = [NSString stringWithFormat:@"%@?%@=%@", [NSString stringWithFormat:kDBCAPTURESIG([orderId intValue])], kAuthToken, authToken];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:url]];
    [client setParameterEncoding:AFJSONParameterEncoding];
    NSDictionary *parameters = @{@"total" : total};
    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"" parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"signature" fileName:@"signature" mimeType:@"image/png"];
    }];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *req, NSHTTPURLResponse *response, id json) {
                                                                                            [submit hide:NO];
                                                                                            if (successBlock) successBlock(req, response, json);
                                                                                        } failure:^(NSURLRequest *req, NSHTTPURLResponse *response, NSError *error, id json) {
                [submit hide:NO];
                if (failureBlock) failureBlock(req, response, error, json);
                NSInteger statusCode = [[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode];
                NSString *alertMessage = [NSString stringWithFormat:@"There was an error processing this request. Status Code: %d", statusCode];
                [[[UIAlertView alloc] initWithTitle:@"Error!" message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
            }];
    [operation start];
}