user393273
user393273

Reputation: 1438

Problem Saving Image

My code is:

NSLog(@"saving jpeg");
NSString *jpegFilePath = [NSString stringWithFormat:@"%@/%@.jpeg",docDir, [self.Youtubearray objectAtIndex:0]];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation([Result1 backgroundImage], 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];

Basically Result1 is a UIButton and i download the image of the button before in the code and set the background image as the downloaded image. Is there a way to save this image from the button or will i have to download it again ?

Thanks

Mason

Upvotes: 0

Views: 707

Answers (3)

henklein
henklein

Reputation: 777

UIImage *img; // your image
NSData *data = UIImageJPEGRepresentation(img, 0.9f);

I'm saving using:

NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];

Upvotes: 0

AechoLiu
AechoLiu

Reputation: 18428

NSData *data = UIImageJPEGRepresentation([Result1 backgroundImage], 1.0f);

If the Result1 cannot obtain the jpeg image data, it will return nil.

Upvotes: 1

Costique
Costique

Reputation: 23722

First of all, the idea of saving a downloaded image into a button doesn't sound very good. You should consider keeping the image somewhere in model/controller part of MVC. A view is not a particularly good place to keep any data because if the view goes away, the data is released too.

Secondly, UIImageJPEGRepresentation() returns an NSData object, so [NSData dataWithData: jpegData] is unnecessary.

Thirdly, I think [Result1 backgroundImage] returns either nil or something like UIStretchableImage. You should check the value returned.

Upvotes: 0

Related Questions