UIImage在iPhone上保存带文件名的图像

时间:2010-02-15 04:27:44

标签: iphone uiimage uiimagepickercontroller sandbox save-image

如何将图像(如使用UIImageWriteToSavedPhotosAlbum()方法)保存为我选择的文件名到private / var文件夹?

3 个答案:

答案 0 :(得分:17)

肯尼,你有答案!为了说明,我总是认为代码更有帮助。

//I do this in the didFinishPickingImage:(UIImage *)img method

NSData* imageData = UIImageJPEGRepresentation(img, 1.0);


//save to the default 100Apple(Camera Roll) folder.   

[imageData writeToFile:@"/private/var/mobile/Media/DCIM/100APPLE/customImageFilename.jpg" atomically:NO]; 

答案 1 :(得分:10)

UIImageWriteToSavedPhotosAlbum()仅用于保存到照片相机胶卷。要保存到自定义文件夹,您需要将UIImage转换为带有UIImageJPEGRepresentation()UIImagePNGRepresentation()的NSData,然后将此NSData保存到您喜欢的任何位置。

答案 2 :(得分:0)

您可以使用以下代码,而无需使用ALAssetsLibrary ...

NSString *fileName;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{

if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
            {
                UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);
                [self performSelector:@selector(GetImageName) withObject:nil afterDelay:0.5];
            }
            else
            {
                NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
                PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
                fileName = [[result firstObject] filename];
            }
}];

-(void)GetImageName
{
 NSString *str =@"";
 PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
 fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
 PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];

if (fetchResult != nil && fetchResult.count > 0) {

    str = [[fetchResult lastObject] filename];
}

fileName = str;
}