【发布时间】:2011-04-07 03:22:37
【问题描述】:
我有一个简单的 iPhone 应用程序,允许用户将图像上传到服务器。问题是,如果他们上传一个大图像文件怎么办。我想将其限制为(最大)200 KB。我开始了一些事情,但它似乎在我的while 声明中崩溃了。
代码如下:
NSString *jpgPath = [NSString stringWithFormat:@"Documents/%@",sqlImageUploadPathTwo];
NSString *jpgPathTwo = [NSString stringWithFormat:@"./../Documents/%@",sqlImageUploadPathTwo];
NSString *yourPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgPath];
NSLog(@"yourPath: %@", yourPath);
NSFileManager *man = [[NSFileManager alloc] init];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
int *result = [attrs fileSize];
NSLog(@"Here's the original size: %d", result);
NSLog(@"jpgPath: %@ // jpgPathTwo: %@", jpgPath, jpgPathTwo);
while (result > 9715) {
UIImage *tempImage = [UIImage imageNamed: jpgPath];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(tempImage, 0.9)];
[imageData writeToFile:jpgPathTwo atomically:YES];
NSLog(@"just shrunk it once.");
}
NSLog(@"SIZE AFTER SHRINK: %@", result);
谢谢!
库尔顿
【问题讨论】:
-
看起来你没有在 while 语句中改变结果,所以你有一个无限循环。
-
我压缩它直到它低于一个设定的限制(在这个例子中,9715字节)。压缩后是否需要拨打
result?谢谢! -
不,我的意思是变量“result”在while循环中没有改变,所以如果它最初是> = 9715,循环永远不会结束,你会遇到堆栈溢出崩溃。
-
所以如果我只是调用它来更新保存后循环中的
result,它会起作用吗? -
还有几个问题 - 查看答案。
标签: objective-c xcode ios4 uiimage