我正在开发一个应用程序,我需要捕获多个图像并将它们存储在一个文件夹中。当图像超出时,应用程序崩溃。而在拍摄图像时,iPhone会慢一点。请有人帮我这个。
捕捉图像的代码
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentModalViewController:picker animated:YES];
将图像添加到文件中的代码
NSString *pngImagePath =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:[NSString
stringWithFormat:@"%@.png",imageName]];
[dataImage writeToFile:pngImagePath atomically:YES];
答案 0 :(得分:0)
此处,以此代码为例,在您将图像显示在屏幕上之前调整图像大小:
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
请注意,在将图像保存到磁盘之前,还应该缩小图像:
UIImage *image = YOUR_IMAGE;
NSData *imageData = UIImageJPEGRepresentation(image, .06); // Here's the level of compression
答案 1 :(得分:0)
尝试使用@autoreleasepool一次,可能是因为一些内存泄漏。 @autoreleasepool维持一定程度..
@autoreleasepool
{
// any allocation for UIImage if u have just bring inside the autoreleasepool
NSString *pngImagePath =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:[NSString
stringWithFormat:@"%@.png",imageName]];
[dataImage writeToFile:pngImagePath atomically:YES];
}