图片压缩保存处理

 
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize {
    CGSize imageSize = image.size;//取出要压缩的image尺寸
    CGFloat width = imageSize.width;    //图片宽度
    CGFloat height = imageSize.height;  //图片高度
    CGFloat scale = height/width;
    
    // Create a graphics image context
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width,newSize.width *scale));
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.width *scale)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}
//存储图像
//在上面我们获取到了图片并对图片进行了压缩,通过之前的小知识了解到,将应用需要的一些图片存入沙盒是个不错的选择,而且应用程序可以直接通过路径去方法沙盒中的图片,在这里我们将图片存入沙盒中的Documents目录下。

#pragma mark 保存图片到document
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
}
//从Documents目录下获取图片

//#pragma mark 从文档目录下获取Documents路径
//- (NSString *)documentFolderPath {
//    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//}
//
////上传图片
//
//- (void)upLoadSalesBigImage:(NSString *)bigImage MidImage:(NSString *)midImage SmallImage:(NSString *)smallImage {
//
//}

你可能感兴趣的:(图片压缩保存处理)