您的位置:首页 > 移动开发 > IOS开发

开发笔记 那些年追过的图片(六):压缩图片并保存到沙盒里

2013-09-12 17:11 507 查看
站在巨人的肩膀我们会看见山后面的海

#pragma mark-
#pragma mark 照片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    
    
    _imageSTring = [Photoimage2String:image];
    imageChange = YES;
    [_login.portraitBtnsetBackgroundImage:imageforState:UIControlStateNormal];
    
    UIImage *headImage = [self imageWithImageSimple:imagescaledToSize:CGSizeMake(160,160)];
    
    [self  saveImageToDocument:headImageimageName:@"image.png"];
    
    //关闭相册界面
    [picker dismissModalViewControllerAnimated:YES];
}

//图片保存到沙盒里

-(void)saveImageToDocument:(UIImage *)image imageName:(NSString *)imagename
{
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *filePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:imagename,nil]];
 // 保存文件的名称
    [UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];
}
//压缩图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
  {
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
      }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios image