当我上传到服务器时,UIImageWriteToSavedPhotosAlbum 图片使我的应用程序崩溃

发布于 2024-08-20 18:48:33 字数 2187 浏览 5 评论 0原文

我正在创建的应用程序以四种方式上传图像(第四种是测试功能);

1.直接从 UIImagePickerController 使用 UIImagePickerControllerSourceTypeCamera 如下:

//didFinishPickingImage method
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}

CGRect myImageRect = CGRectMake(30.0f, 190.0f, 250.0f, 210.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
//released in dealloc because I reuse it alot

myImage.image = img;
[self.view addSubview:myImage]; 

2.直接从照片库中选取也使用

myImage.image = img;

3.使用从 1 保存的图像。

pathFile = @"/private/var/mobile/Media/DCIM/100APPLE/"; 
NSString *ImagePath = [pathFile stringByAppendingString:imageFileName];
NSData *imageDataFile = [NSData dataWithContentsOfFile:ImagePath];  

UIImage *img = [UIImage imageWithContentsOfFile:ImagePath];

myImage.image = img;

4.作为测试功能:使用使用 iPhone 默认相机应用程序拍摄的图像(与 UIImagePickerController 相同)但使用 ImagePath 而不是 UIImagepickerController、UIImagePickerControllerSourceTypePhotoLibrary。

//Same as Number 3.
pathFile = @"/private/var/mobile/Media/DCIM/100APPLE/"; 
NSString *ImagePath = [pathFile stringByAppendingString:imageFileName];
NSData *imageDataFile = [NSData dataWithContentsOfFile:ImagePath];  

UIImage *img = [UIImage imageWithContentsOfFile:ImagePath];

myImage.image = img; 

*到目前为止,用户可以在屏幕上以四种方式中的任何一种查看添加到 UIImageView 的 myImage 实例中的文件(取决于用户的选择)。这证明我选择了正确的文件(三个和四个)。

//uploading function

NSData *imageData = UIImageJPEGRepresentation(myImage.image, 1.0);


NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease];
            [request2 setURL:[NSURL URLWithString:urlString]];
            [request2 setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];
//... append boundary, content-disposition code etc here.
[body appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:body];

问题是,上传时,数字 1,2 和 4 有效,数字 3 无效。

控制台输出; 程序收到信号:“EXC_BAD_ACCESS”

我知道此输出源于尝试访问已释放的对象,并且我在其他情况下遇到并解决了此问题。但即使在检查了我的内存分配之后,仍然没有找到办法。

我真的很感激解决这个问题的方法。

The app that I am creating uploads images in four(the fourth is a test function) ways;

1.Directly from UIImagePickerController with UIImagePickerControllerSourceTypeCamera like this:

//didFinishPickingImage method
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}

CGRect myImageRect = CGRectMake(30.0f, 190.0f, 250.0f, 210.0f); 
myImage = [[UIImageView alloc] initWithFrame:myImageRect];
//released in dealloc because I reuse it alot

myImage.image = img;
[self.view addSubview:myImage]; 

2.Directly picking from photo library also using

myImage.image = img;

3.Using the image saved from 1.

pathFile = @"/private/var/mobile/Media/DCIM/100APPLE/"; 
NSString *ImagePath = [pathFile stringByAppendingString:imageFileName];
NSData *imageDataFile = [NSData dataWithContentsOfFile:ImagePath];  

UIImage *img = [UIImage imageWithContentsOfFile:ImagePath];

myImage.image = img;

4.As a test function: Using an image taken with the iPhone default camera app (same as UIImagePickerController) but using ImagePath instead of UIImagepickerController, UIImagePickerControllerSourceTypePhotoLibrary.

//Same as Number 3.
pathFile = @"/private/var/mobile/Media/DCIM/100APPLE/"; 
NSString *ImagePath = [pathFile stringByAppendingString:imageFileName];
NSData *imageDataFile = [NSData dataWithContentsOfFile:ImagePath];  

UIImage *img = [UIImage imageWithContentsOfFile:ImagePath];

myImage.image = img; 

*Up to here, the user can see the file in any of the four ways on the screen added to myImage instance of UIImageView(Depending on the user selection). This proves that I am selecting the right files(for three and four).

//uploading function

NSData *imageData = UIImageJPEGRepresentation(myImage.image, 1.0);


NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease];
            [request2 setURL:[NSURL URLWithString:urlString]];
            [request2 setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];
//... append boundary, content-disposition code etc here.
[body appendData:[NSData dataWithData:imageData]];
[request setHTTPBody:body];

The problem is that, on upload, number 1,2 and 4 work, NUMBER 3 DOESN'T.

Console output;
Program received signal: "EXC_BAD_ACCESS"

I am aware that this output stems from attempted access from released objects, and I have run into and solved this problem in other situations. But found no way here yet, even after counterchecking my memory allocations.

I would REALLY appreciate ways to solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2024-08-27 18:48:33

iPhone OS 创建了另一个与默认 100APPLE 不同的文件夹“/private/var/mobile/Media/DCIM/101APPLE/”。

我意识到实际上有时照片也会保存在999APPLE、102APPLE等文件夹中。这取决于您如何在相机胶卷中删除、复制和创建照片。

因此,您的相机胶卷照片实际上可以存储在许多不同的文件夹中。

因此,无法执行 [NSData dataWithContentsOfFile:ImagePath];,它会抛出异常:

“EXC_BAD_ACCESS”

iPhone OS had created another folder "/private/var/mobile/Media/DCIM/101APPLE/" different from the default 100APPLE.

I realized that actually sometimes the photos are also saved in a folder 999APPLE, 102APPLE etc. This depends on how you delete, copy and create photos in the Camera Roll.

So your Camera Roll pictures could actually be stored in many different folders.

Therefore, not being able to execute [NSData dataWithContentsOfFile:ImagePath];, it throws exception:

"EXC_BAD_ACCESS"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文