2

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

在此过程中,用户在保存数据的同时,还可以保存联系人图片(字符串形式,图片文件名)

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

- (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];
}

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

但是,当我保存的图像超过 6/7 时,它会导致内存警告,并且应用程序会崩溃/变慢。*

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

怎么可能,谢谢

4

2 回答 2

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();

希望对你有帮助..

于 2012-11-26T08:01:51.840 回答
3

用这个怎么样

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25     orientation:original.imageOrientation];

并将较小的图像保存到 tmp 文件路径以供上传。图片必须是.png吗?否则,您也可以尝试 UIImageJPEGRepresentation 来降低图像的质量。

于 2012-11-26T07:55:51.747 回答