开发者

Adding images into the iPhone application

开发者 https://www.devze.com 2023-03-15 23:16 出处:网络
I have an application where the user 开发者_StackOverflowshould be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album

I have an application where the user 开发者_StackOverflowshould be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album.But I want to bring them into my project just like the way we add required images for the project into the Resources folder in Xcode. At the same time I want them to converted into PNG format.


After you have taken image from Camera you can save this image to your application's document directory as,

UIImage *image = imageFromCamera;
    NSData *imageData = UIImagePNGRepresentation(image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"MyImage.png"];
    [imageData writeToFile:fullPathToFile atomically:YES];

Its tested code and working absolutely fine.


here is a great tutorial that you should check out http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html

And here is the documentation from Apple for the UIImagePickerController that you should use https://developer.apple.com/documentation/uikit/uiimagepickercontroller


NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *DocsDir = [UsrDocPath objectAtIndex:0];

    NSString *path = [DocsDir stringByAppendingPathComponent:@"image.jpg"];

//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
    BOOL success = [FileManager fileExistsAtPath:zipPath];

    if(success){
        [FileManager removeItemAtPath:path error:&error];
    }

    [UIImageJPEGRepresentation(image,1.0) writeToFile:path atomically:YES];
0

精彩评论

暂无评论...
验证码 换一张
取 消