[英]App Crash while loading images in iPhone
我從相機中挑選圖像並將其保存在本地文件夾中。我在滾動查看器中顯示保存的圖像。 當我嘗試加載超過 5 張圖像時,應用程序崩潰並出現 memory 警告。
在這里查看我的源代碼:
for (int Count = 0; Count < [listData count] ; Count ++)
{
Photo *photo = [listData objectAtIndex: Count];
if([FileUtils fileExistsAtPath:photo.Path fileName:photo.PhotoName])
{
PhotoView *photoView = [[PhotoView alloc] initWithFrame: CGRectMake(ThumbnailSizeWidth * (PhotoViewCount % THUMBNAIL_COLS) + PADDING * (PhotoViewCount % THUMBNAIL_COLS) + PADDING,
ThumbnailSizeHeight * (PhotoViewCount / THUMBNAIL_COLS) + PADDING * (PhotoViewCount / THUMBNAIL_COLS) + PADDING + PADDING_TOP,
ThumbnailSizeWidth,
ThumbnailSizeHeight)];
[photoView setDelegate:self];
[photoView setPhoto:photo];
[photoView setTagIndexID:OrginalCounter];
//NSLog(@"TagIndexID:%d",Count);
PhotoViewCount ++ ;
if(photo.isPrivacy)
{
UIImage *tImage = [UIImage imageNamed:@"locked.png"];
[photoView setPhotoViewImage:tImage];
}
else
{
[photoView setTag:OrginalCounter];
NSData *imgData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]];
UIImage *thumnail = [UIImage imageWithData:imgData];
//UIImage *thumnail = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]];
//UIImage *thumnail = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@%@",photo.Path,photo.PhotoName]];
MyPhoto *photo = [[MyPhoto alloc] initWithImage:thumnail];
[photos addObject:photo];
[photo release];
//[thumnail release];
OrginalCounter++;
[photoView performSelectorInBackground:@selector(setPhotoViewImage:) withObject:thumnail];
}
[scrollViewer addSubview:photoView];
[photoView release];
}
}
-(void) setPhotoViewImage:(UIImage*)image
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
PrivacyPixAppDelegate *appDelegate = [PrivacyPixAppDelegate appDelegate];
image=[appDelegate imageWithImage:image scaledToSize:CGSizeMake(75.0, 104.0)];
//image = [image scaleAndCropToSize:CGSizeMake(69.0, 104.0) onlyIfNeeded:YES];
//image = [image scaleAndCropToSize:CGSizeMake(75, 100)];
//image=[image ScaleImageToRect:image displaySize:CGSizeMake(40,40)];
if(!btnPhoto)
btnPhoto = [[UIImageView alloc] init];
[btnPhoto setFrame:CGRectMake(2, 2, 75, 75)];
//[btnPhoto setContentMode:UIViewContentModeTop];
btnPhoto.image = image;
[self addSubview:btnPhoto];
//[btnPhoto release];
if(!txtPhotoName)
txtPhotoName = [[UITextField alloc] init];
[txtPhotoName setDelegate:self];
[txtPhotoName setFrame:CGRectMake(2, btnPhoto.frame.size.height + 2, self.frame.size.width, 20)];
txtPhotoName.font = [UIFont boldSystemFontOfSize:12.0];
txtPhotoName.backgroundColor = [UIColor whiteColor];
txtPhotoName.textAlignment = UITextAlignmentCenter;
txtPhotoName.borderStyle = UITextBorderStyleLine;
txtPhotoName.text = photo.PhotoCaption;
txtPhotoName.returnKeyType = UIReturnKeyDone;
txtPhotoName.hidden = YES;
[self addSubview:txtPhotoName];
if(!lblPhotoName)
lblPhotoName = [[UILabel alloc] init];
[lblPhotoName setFrame:CGRectMake(0, btnPhoto.frame.size.height, self.frame.size.width, 25)];
lblPhotoName.backgroundColor = [UIColor clearColor];
lblPhotoName.font = [UIFont systemFontOfSize:9.0];
lblPhotoName.userInteractionEnabled = NO;
lblPhotoName.text = photo.PhotoCaption;
lblPhotoName.numberOfLines = 2;
lblPhotoName.hidden = NO;
lblPhotoName.textAlignment = UITextAlignmentCenter;
[self addSubview:lblPhotoName];
//[lblPhotoName release];
[pool release];
}
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
如果您因 memory 警告而崩潰,這一定是因為您的圖像太大。 您應該在屏幕上顯示它們之前調整它們的大小,這樣它們占用的 memory 空間就會更少。 您可以通過使用這篇文章中的類別來做到這一點: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
在我看來,您不應該將圖像數據加載到 memory 中,這會導致低 memory 崩潰,只需保留 imageName 和 imagePath。
對於 setPhotoViewImage: function 只需傳遞 imageName 和 imagePath 而不是 uiimage,並在需要圖像時直接加載圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.