如何在保存或在iphone中捕获图像时降低图像分辨率和内存大小

use*_*427 2 iphone objective-c uiimageview uiimagepickercontroller uiimage

在我的联系人应用程序中,我使用图像视图来显示联系人图像,

在此过程中,保存数据用户也可以保存联系人图像(以字符串形式,图像文件名).

我将图像复制到沙箱(文档目录)中并保存图像的文件名

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{

            NSString *DirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            [[NSUserDefaults standardUserDefaults] setValue:storedPicsDict.contactImage forKey:@"oldContactPic"];

            //To SET the NEw IMAGE images from directory path
            CFUUIDRef uuid = CFUUIDCreate(NULL);
            CFStringRef generatedUUIDString = CFUUIDCreateString(NULL, uuid);
            CFRelease(uuid);
            NSString* hashKey = [(NSString*)generatedUUIDString autorelease];
            self.ContactImageFilePath = [NSString stringWithFormat:@"%@/%@.png",DirectoryPath,hashKey];

            storedPicsDict.contactImage = self.ContactImageFilePath;
            [contactPicture setImage:image forState:UIControlStateNormal];

            isNewContactImage = true;
      }


      [picker dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)

相应的保存图像将显示在联系人的信息中.

但是,如果我保存了超过6/7的更多图像,它会导致内存警告,并且应用程序会崩溃/减速.*

所以我需要使用LOW RESOUTION和LOW MEMORY SIZE保存图像,

怎么可能,谢谢

Mih*_*hir 11

1>以jpeg格式保存图像,以便减小图像尺寸

NSData *imgData = UIImageJPEGRepresentation(your_UIImage_to_save, 0.5);
[imgData writeToFile:path_of_doc_dir_with_image_name atomically:YES];
Run Code Online (Sandbox Code Playgroud)

2>

如果要降低图像的质量和大小,可以使用此代码

CGSize newSize=CGSizeMake(50,50); // I am giving resolution 50*50 , you can change your need
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助..