在我的通讯录应用程序中,我使用图像视图显示联系人图像,
在此过程中,保存数据用户可以保存联系人图像(以字符串,图像文件名称的形式)。
我在沙盒中复制图像(文档目录)并保存图像的文件名
- (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];
}
相应的保存图像将显示在联系人的信息中。
但是w * hen我保存了超过6/7的更多图像,它会导致内存警告,并且应用程序会崩溃/减速。 *
所以我需要使用LOW RESOUTION和LOW MEMORY SIZE保存图像,
怎么可能,谢谢
答案 0 :(得分:11)
1>以jpeg格式保存图像,以便缩小图像尺寸
NSData *imgData = UIImageJPEGRepresentation(your_UIImage_to_save, 0.5);
[imgData writeToFile:path_of_doc_dir_with_image_name atomically:YES];
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();
希望这会对你有帮助..
答案 1 :(得分:3)
如何使用此
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
并将较小的图像保存到tmp文件路径以进行上载。图像需要是.png吗? 否则,您也可以尝试UIImageJPEGRepresentation来降低图像的质量。