how can i get image from UIPasteboard after the user click copy actually i can get Nsdata from UIPasteboard but i think it needs specific coding to be assigned to uiimageview how can i do this ? thanks for help.
NSData *data = // however you obtain the data
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Even better:
UIImage *image = [[UIPasteboard generalPasteboard] image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Related
I am trying to create an app like this:
(source: topmobiletrends.com)
where each of the views show an image for every in a photo in a JSON array.
I have successfully set and downloaded the array of images and set them in a UIImageView with with NSURLRequest and SDWebImage.
I created a UIView with an image view inside to test the code in my ViewController(Storyboard), everything works fine. However, when I try to create a UIView to loop programmatically, it shows up blank, with no ImageView. I'm not sure what I'm doing wrong.
Here is my code for my ViewController.m
for (NSObject *photo in photos) {
UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
dView.backgroundColor = [UIColor whiteColor];
dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
myImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Get image from url
[self.imageView setImageWithURL:imageURL];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
[imageView addSubview:dView];
});
}
}];
[task resume];
}
you are adding dView as subview for your image view may be it is [dView addSubview:imageView];
try this. the main issue is you are not setting frame for image view
for (NSObject *photo in photos) {
//UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
//dView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160,250,258,229)];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
//[imageView addSubview:dView];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"URL"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData) {
[imageView setImage:[UIImage imageWithData:imgData]];
}
});
});
}
I use the following code to create borders around images. I loop through the following multiple times to create thumbnail images with borders that are then placed in a UICollectionView.
The problem seems to be that every time I reload the UICollectionView the images are not being released from memory and it seems to build up to the point where it crashes. I don't think it is the UICollectionView code because If I run it with images that don't require a border I don't get any issues.
- (UIImage *)applyFrame:(UIImage *)image selectedFrame:(NSString *)selectedFrame {
UIImage *frame;
NSLog(#"Image Size For Frames: %f, %f",image.size.width,image.size.height);
if(image.size.width == image.size.height){
frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#-Square.png",selectedFrame] ofType:nil]];
}
if(image.size.width > image.size.height){
frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.png",selectedFrame] ofType:nil]];
}
if(image.size.width < image.size.height){
frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.png",selectedFrame] ofType:nil]];
frame = [self rotateUIImage:frame clockwise:true];
}
GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
GPUImagePicture *imageToProcess = [[GPUImagePicture alloc] initWithImage:image];
GPUImagePicture *border = [[GPUImagePicture alloc] initWithImage:frame];
blendFilter.mix = 1.0f;
[imageToProcess addTarget:blendFilter];
[border processImage];
[border addTarget:blendFilter];
[imageToProcess processImage];
return [blendFilter imageFromCurrentlyProcessedOutput];
}
- (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise {
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UIImage imageWithContentsOfFile: returns an autorelease object, which will be released only when the app returns to the current run loop. So, might it be that you app loops for a long time executing this code? In this case, the autorelease objects might accumulate, and increase the heap usage.
If so, you could try to insert into the body of the responsible loop an autorelease block:
#autoreleasepool {
...your statements
}
I call the following method to get an image which I display in a UIImageView. When I am done with the UIImageView I release myImageView.image. I am happy with how I do this but Instruments is showing a leak of UIImage. How should I be releasing the below instance of UIImage?
-(UIImage *)getImageWithRef:(NSString *)ref {
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"foobar_%#",ref]];
if (myImage == nil) {
myImage = [[UIImage alloc] initWithContentsOfFile:#"foobar_defaultImage"];
}
return myImage;
}
Thanks in advance
Try using auto-release when you return your image.
-(UIImage *)getImageWithRef:(NSString *)ref {
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"foobar_%#",ref]];
if (myImage == nil) {
myImage = [[UIImage alloc] initWithContentsOfFile:#"foobar_defaultImage"];
}
return [myImage autorelease];
}
I'm a novice in this, so i would like to know what is missing in my code to download/show these images:
self.carView.contentSize = CGSizeMake(280*self.carImages.count-280, 200);
self.carView.pagingEnabled = YES;
CGRect imageRect = CGRectMake(0.0, 0.0,280*self.carImages.count-280, 200);
UIGraphicsBeginImageContext(imageRect.size);
UIImageView *carImg = [[UIImageView alloc] init];
for (int i = 1; i < self.carImages.count; i++) {
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self.carImages objectAtIndex:i]]];
UIImage *carImage = [[UIImage alloc] initWithData:imageData];
[carImage drawAtPoint:CGPointMake(280*(i-1), 0)];
[carImage release];
[imageData release];
NSLog(#"%d",i);
}
carImg.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.carView addSubview:carImg];
[carImg release];
carView is my ScrollView.
carImages is a MutableArray with the "URL" of the images.
the pagingEnabled and contentSize are being set, but no images.
I bet i'm missing something very stupid, as i allways do.
I used the heapshot analysis, and it pointed to the first two lines reproduced below. It seems like I am releasing all the memory I am allocating. I don't see how the memory is accumulating.
NSData* data = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
UIImage *view = [[UIImage alloc] initWithData:data];
background = [[UIImageView alloc] initWithImage:view];
[view release];
[display addSubview: background];
if(fileExists){
[background addSubview: newView];
}
[background release];