当PFFile太大时,应用程序崩溃

时间:2022-09-06 20:55:32

My app crashes when I try to upload a PFFile that is larger than 10mb. I thought that Parse would catch the error and I could display an alert view at this point, but instead the app just crashes.

当我试图上传大于10mb的PFFile时,我的应用程序崩溃了。我认为解析会捕获错误,我可以在此时显示一个警告视图,但是应用程序崩溃了。

My code below works fine for saving images, but like I said, it crashes if the file is too big.

我下面的代码可以很好地保存图像,但是就像我说的,如果文件太大,它就会崩溃。

I have tried to catch the error with if (error) statements, but no luck.

我试图用if (error)语句来捕获错误,但没有成功。

PFQuery *query = [PFQuery queryWithClassName:@"Football_Clubs"];

    [query getObjectInBackgroundWithId:objectId block:^(PFObject *object, NSError *error)
     {
         NSData *imageData = UIImagePNGRepresentation(badgeImage);
         PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
         object[@"badge_image"] = imageFile;

         [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
         {
             if (succeeded)
             {
                 [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                     if (succeeded)
                     {
                         badgeImageView.image = nil;
                         [getSettingsQuery clearCachedResult];
                         [self loadSettings];
                         [progress setText:@"Saved"];
                     }
                 }];
             }
         } progressBlock:^(int percentDone)
         {
             [progress setText:[NSString stringWithFormat:@"Saving image - %d%@ complete", percentDone, @"%"]];
         }];
     }];

My console log is as follows:

我的控制台日志如下:

2014-07-31 12:40:47.466 Sporter[21017:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFFile cannot be larger than 10485760 bytes' * First throw call stack: (0x18ae1ef50 0x1973281fc 0x18ae1ee90 0x10011d550 0x1000c6ae4 0x100141334 0x197900014 0x1978fffd4 0x1979031dc 0x18addec2c 0x18addcf6c 0x18ad1dc20 0x190a05c0c 0x18de4efdc 0x1000d5218 0x19791baa0) libc++abi.dylib: terminating with uncaught exception of type NSException

2014-07-31 12:40:47.466 Sporter[21017:60b] *由于“NSInvalidArgumentException异常”而终止app,原因是“PFFile不能大于10485760 bytes”* First throw call stack:dylib:终止与未捕获的类型NSException异常。

2 个解决方案

#1


8  

The size limit of a PFFile object is 10MB

PFFile对象的大小限制为10MB

The PFFile

的PFFile

PFFile lets you store application files in the cloud that would otherwise be too large or cumbersome to fit into a regular PFObject. The most common use case is storing images but you can also use it for documents, videos, music, and any other binary data (up to 10 megabytes).

PFFile允许您将应用程序文件存储在云中,否则,这些文件太大或太麻烦,无法装入常规的PFObject。最常见的用例是存储图像,但是您也可以将它用于文档、视频、音乐和任何其他二进制数据(最多10mb)。

Taken from the iOS guide on PFFile, https://parse.com/docs/ios_guide#files/iOS

摘自PFFile的iOS指南,https://parse.com/docs/ios_guide#files/iOS

Reference answer from here, https://*.com/a/16729142/1603234

参考答案,https://*.com/a/167292/1603234

In your case, 10485760 bytes means 10.48576 megabytes. So that's maximum file size.

在您的例子中,10485760字节意味着10.48576兆字节。这就是最大文件大小。

How to get a file size?

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:yourFilePath error:nil];
unsigned long long fileSize = [attributes fileSize]; // result would be in bytes

if(fileSize <= 10485760) {
    //you can continue for upload.
}else{
   //file size exceeding, can't upload.
}

However this is static, and not suggestible because if PARSE will change his mind and allowing double sized files to upload!

但是,这是静态的,并且没有暗示,因为如果PARSE将改变主意,允许双大小的文件上传!

#2


1  

Example in Swift:

在斯威夫特的例子:

func imageFileSize() {
    let data: NSData = NSData(contentsOfURL: NSURL(string: myURL)!)!
    println("\(data.length) bytes")
    println("\( Double(data.length) * pow(Double(10.0), Double(-6.0)) ) MB") // MB: Megabytes
    println("\( Double(data.length) / pow(Double(2.0), Double(20.0)) ) MiB") // MiB: Mebibytes
    println("isFileSizeUpTo10Mebibytes(\(data.length)) returns: \(isFileSizeUpTo10Mebibytes(data.length))")
}

/* return true if fileSize <= 10485760 bytes
   else return false */
func isFileSizeUpTo10Mebibytes(fileSize: Int) -> Bool {
    return fileSize <= 10485760
}

Moreover there is an error in the iOS/OSX SDK documentation < Files part < The PFFile:

此外,iOS/OSX SDK文档中有一个错误 <文件部分< pffile:< p>

当PFFile太大时,应用程序崩溃

(Source of this information)

(此信息来源)

It is 10 Mebibytes and not 10 Megabytes.

它是10兆字节而不是10兆字节。

#1


8  

The size limit of a PFFile object is 10MB

PFFile对象的大小限制为10MB

The PFFile

的PFFile

PFFile lets you store application files in the cloud that would otherwise be too large or cumbersome to fit into a regular PFObject. The most common use case is storing images but you can also use it for documents, videos, music, and any other binary data (up to 10 megabytes).

PFFile允许您将应用程序文件存储在云中,否则,这些文件太大或太麻烦,无法装入常规的PFObject。最常见的用例是存储图像,但是您也可以将它用于文档、视频、音乐和任何其他二进制数据(最多10mb)。

Taken from the iOS guide on PFFile, https://parse.com/docs/ios_guide#files/iOS

摘自PFFile的iOS指南,https://parse.com/docs/ios_guide#files/iOS

Reference answer from here, https://*.com/a/16729142/1603234

参考答案,https://*.com/a/167292/1603234

In your case, 10485760 bytes means 10.48576 megabytes. So that's maximum file size.

在您的例子中,10485760字节意味着10.48576兆字节。这就是最大文件大小。

How to get a file size?

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:yourFilePath error:nil];
unsigned long long fileSize = [attributes fileSize]; // result would be in bytes

if(fileSize <= 10485760) {
    //you can continue for upload.
}else{
   //file size exceeding, can't upload.
}

However this is static, and not suggestible because if PARSE will change his mind and allowing double sized files to upload!

但是,这是静态的,并且没有暗示,因为如果PARSE将改变主意,允许双大小的文件上传!

#2


1  

Example in Swift:

在斯威夫特的例子:

func imageFileSize() {
    let data: NSData = NSData(contentsOfURL: NSURL(string: myURL)!)!
    println("\(data.length) bytes")
    println("\( Double(data.length) * pow(Double(10.0), Double(-6.0)) ) MB") // MB: Megabytes
    println("\( Double(data.length) / pow(Double(2.0), Double(20.0)) ) MiB") // MiB: Mebibytes
    println("isFileSizeUpTo10Mebibytes(\(data.length)) returns: \(isFileSizeUpTo10Mebibytes(data.length))")
}

/* return true if fileSize <= 10485760 bytes
   else return false */
func isFileSizeUpTo10Mebibytes(fileSize: Int) -> Bool {
    return fileSize <= 10485760
}

Moreover there is an error in the iOS/OSX SDK documentation < Files part < The PFFile:

此外,iOS/OSX SDK文档中有一个错误 <文件部分< pffile:< p>

当PFFile太大时,应用程序崩溃

(Source of this information)

(此信息来源)

It is 10 Mebibytes and not 10 Megabytes.

它是10兆字节而不是10兆字节。